i have a numeric field with no decimals that contains the age of an individual. i’m trying to compare it to the age 16 for a guardianship validation. i have tried all kind of javascript formats and can’t figure it out. can anyone help?
this is the code i am using.
//Validate Guardian First Name
if (parseInt(document.forms[0].Age.value) < 16 && document.forms[0].guardianFirstName.value=="")
{
window.alert('You must enter a Guardian First Name!')
document.forms[0].guardianFirstName.focus()
}
//Validation Complete
else
{
document.forms[0].submit()
}
Subject: javascript numeric validation
I think this is more-or-less what you’re looking for:
function validate() {
var f = document.forms[0];
var age = parseInt(f.Age.value);
if (isNaN(age)) {
alert("You must enter your age as a number.");
f.Age.focus();
return false;
}
else {
if (age<16) {
if (f.GuardianFirstName.value == "") {
alert("You must enter your legal guardian's First Name.");
f.GuardianFirstName.focus();
return false;
}
else if (f.GuardianLastName.value == "") {
alert("You must enter your legal guardian's Last Name.");
f.GuardianLastName.focus();
return false;
}
}
return true;
}
}