Looking for a simple form validation script:
<script language=”javascript”>
function checkForm(register) {
if (""==document.forms.register.FNAME.value){
alert("Please fill out this field!");
document.forms.register.FNAME.focus();
return false;
}
if (""==document.forms.register.LNAME.value){
alert("Please fill out this field!");
document.forms.register.LNAME.focus();
return false;
}
if (EMAIL.value.search( /^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/ ) == -1){
alert(“Invalid email”);
return false;
}
if('0'==document.forms.register.GENDER.value){
alert("Please select an option!");
document.forms.register.GENDER.focus();
return false;
}
if (""==document.forms.register.ADDRESS.value){
alert("Please fill out this field!");
document.forms.register.ADDRESS.focus();
return false;
}
if (""==document.forms.register.CONTACTNO.value){
alert("Please fill out this field!");
document.forms.register.CONTACTNO.focus();
return false;
}
}
</script>
Using the onSubmit handler to call the function, but nothing is happening when submit is clicked. It's skipping javascript and going straight to the PHP script. Any ideas why?
Form HTML:
<form name="register" action="register.php" method="POST" onsubmit="return checkForm(register);">
<table width="100%" border="0">
<tr>
<td width="46%" height="24" align="right">First Name:</td>
<td width="54%"><input name="FNAME" type="text" size"20" /></td>
</tr>
<tr>
<td height="24" align="right">Last Name:</td>
<td><input name="LNAME" type="text" size"20" /></td>
</tr>
<tr>
<td height="24" align="right">Email Address</td>
<td><input name="EMAIL" type="text" size"20" /></td>
</tr>
<tr>
<td height="24" align="right">Gender:</td>
<td><select name="GENDER">
<option value="" selected="selected">- Select One -</option>
<option value="Male">Male</option>
<option value="Female">Female</option></select></td>
</tr>
<tr>
<td height="24" align="right">Address:</td>
<td><input name="ADDRESS" type="text" size"20" /></td>
</tr>
<tr>
<td height="24" align="right">Contact No.:</td>
<td><input name="CONTACTNO" type="text" size"20" /></td>
</tr>
<tr>
<td height="24" align="right">Password</td>
<td><input name="PASSWORD" type="password" size"20" /></td>
</tr>
<tr>
<td height="24" align="right">Re-type Password</td>
<td><input name="PASSWORD2" type="password" size"20" /></td>
</tr>
<tr>
<td> </td>
<td><input name="submit" type="submit" value="Register" /></td>
</tr>
</table>
</form>
Not seeing the alert message? What could be causing this issue?