PDA

View Full Version : JavaScript Form Validation



809AreaCode
17 May 2011, 08:55 PM
Hi, I'm new to the forums, and web design/development. Anyway, I am trying to write a JS that validates a form... I'm kind of frustrated with one part of it...


<tr><td class="alignRight">Enter Age</td><td class="alignLeft"><input type="text" name="age" /></td></tr>

I'm trying to check document.myForm.age and make sure that no characters other than numbers are in the field, that the value is no greater than 150 and no less than 13. Here's what I've come up with:


if((document.myForm.age.test([1-9]))==true){//my problem is here!

if((document.myForm.age.value)>(150)){

alert("Come on... no one's that old!");

return false;

}

if((document.myForm.age.value)<(13)){

alert("Sorry, you're too young to have an account!");

return false;

}

}else{

alert("You put letters in the age field.");

return false;

}
}
All of it works... except for the first line. Can anyone tell me how they check text for numbers? Thanks!

FreshpageDesign
18 May 2011, 10:47 AM
Use isNaN() to check if it's a valid number
http://www.w3schools.com/jsref/jsref_isNaN.asp