Q ) Below is my code & I keep getting an error , where am I wrong ? And I am also not getting the age validation part right. I want it to be a number & between 1& 100, how to write that ???
<HTML>
<HEAD>
<TITLE ></TITLE>
<SCRIPT>
// check for valid email address format //
function validEmail(email) {
invalidChars = " /:,;";
if (email == ""
for (i = 0; i < invalidChars.length; i++) {
badChar = invalidChars.charAt(i);
if (email.indexOf(badChar, 0) > -1) return false;
}
atPos = email.indexOf("@", 1);
if (atPos == -1) return false;
if (email.indexOf("@", atPos + 1) != -1) return false;
periodPos = email.indexOf(".", atPos);
if (periodPos == -1) return false;
if (email.lastIndexOf("."
if (email.length > email.lastIndexOf("."
return true;
}
// check for valid Postal code which is either 5 or 9 digit zip with a hyphen after 5 digits.//
function validateZIP(zipfield) {
var valid = "0123456789-";
var hyphencount = 0;
if (field.length!=5 && field.length!=9 && field.length!=10) {
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-"
if (valid.indexOf(temp) == "-1"
return false;
}
if ((field.length ==9) | | (field.length == 10)){
if ((hyphencount != 1) && (field.charAt(5)!="-"
return false;
}}
}
return true;
}
// Check if name fields are valid characters//
function alphaOnly(theString) {
var OK = true;
for (var i=0;i<theString.length;i++) {
theChar = theString.charAt(i);
if ( (theChar >= "a"
continue;
if ( (theChar >= "A"
continue;
OK = false;
}
return OK;
}
// check if number fields are numeric//
function numbersOnly(theString) {
var OK = true;
for (var i=0;i<theString.length;i++) {
theChar = theString.charAt(i);
if ((theChar < "0"
OK = false;
break;
}
}
return OK;
}
//----------------------------------------------------//
function ValidateInput(form) {
var LB = "\n"; // my notation for Line Break
var msghdr = "Please fill out your:" + LB + LB;
var msg = "";
if (!form.First.value) msg += "First Name" + LB;
else if (!alphaOnly(form.First.value)) msg += "Invalid First Name" + LB;
if (!form.Last.value) msg += "Last Name" + LB;
else if (!alphaOnly(form.Last.value)) msg += "Invalid Last Name" + LB;
if (!form.Address.value.length == 0) msg += "Address" + LB;
if (!form.City.value) msg += "City" + LB;
else if (!alphaOnly(form.City.value)) msg += "Invalid City Name" + LB;
if (!form.State.value) msg += "State" + LB;
else if (!alphaOnly(form.State.value)) msg += "Invalid State Code" + LB;
if (!form.Postal.value) msg += "Postal Code" + LB;
else if (!validateZIP(form.Postal.value)) msg += "Invalid Postal Code" + LB;
if (form.Age.value.length == 0 || parseInt(form.Age.value) == 0) msg += "Age" + LB;
else if (!numbersOnly(form.Age.value)) msg += "Invalid Age" + LB;
if (!form.Income.options[form.Income.selectedIndex].value == "0"
if (form.Sex[0].checked == false && form.Sex[1].checked == false) msg += "Sex" + LB;
if (!form.EMail.value){
msg += "Email Address" + LB;
}
else if (!validEmail(form.EMail.value)) {
msg += "Invalid Email Address" + LB;
}
// Display an alert if any of the input is missing:
if (msg.length > 0){
alert(msghdr + msg);
return false;
}
else return true;
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF">