Page 1 of 1

only allow digits in textfield

Posted: Tue Feb 14, 2006 2:38 am
by zh2908
hi, i'm trying to only allow numbers for my textfield.

right now, my script only notifies when the textfield is left blank. how can i change the codes, so that the textfield only accepts 8 digits (12345678) and nothing else?

Code: Select all

<script type="text/javascript"> 

      //--> 

var errormessage = "" 
var passme=false    
    
function validate() 
{ 
if(passme){passme=false;return true} 

        errormessage = "" 
        


   Contact()

    
   if (errormessage == "") 
   { 
      return (true);    
   } 
   else 
   { 
      alert(errormessage) 
             return (false); 
    } 
} 

function Contact() 
{ 
   var cont_no = window.document.contest.contact_no.value 
   if (cont_no == "") 
   { 
      errormessage = errormessage + "Please enter your contact number.\n" 
   } 
} 




   </script>

Posted: Tue Feb 14, 2006 2:42 am
by feyd
sounds like a job for regex.

Code: Select all

field.value.match(/^[0-9]{8}$/)
If I remember right..

Posted: Tue Feb 14, 2006 3:01 am
by zh2908
did you mean to replace
var cont_no = window.document.contest.contact_no.value

with

var cont_no = window.document.contest.contact_no.value.match(/^[0-9]{8}$/)

? it doesnt seem to work...