Click to See Complete Forum and Search --> : Another Form Validation Question...


ozz
03-31-2003, 06:16 AM
How would I go about validating this field so that it only accept exactly 12 numbers

if (document.form.idnum.value=="") {
window.alert ("Please enter your ID Number!");
document.form.idnum.focus();
return false;
}

if (isNaN(document.form.idnum.value)) {
window.alert ("Your ID Number Must be Numerical and contain 12 Digits!");
document.form.idnum.focus();
return false;
}

hte form validates ok at present for a null value and a numerical value but i would like it to validate for 12 only as well... TIA

khalidali63
03-31-2003, 06:47 AM
you can use an html elements attribute.
input tag has an attribute "maxlength"
you can set it in the text fields html declaration.
For e.g in this case
<input type="text" name ="idnum" maxlength="12"> will take care of that you won't need javascript to validate that.

Cheers

Khalid

ozz
03-31-2003, 07:48 AM
But what happens if someone only types in 10 numbers? The idnum must be exactly 12.

khalidali63
03-31-2003, 09:06 AM
in that case you can add to this line
if (isNaN(document.form.idnum.value)) {

as
if (isNaN(document.form.idnum.value) && document.form.idnum.value.length<12

and so on