Forum Moderators: open
I am trying to validate a form but when I click Submit nothing happens. here is an example. pls help
// JavaScript Document
<script Language="JavaScript">
<!--
function Form_Validator(theForm)
{
var alertsay = "";
if (theForm.Firstname.value == "")
{
alert("You must enter a First name.");
theForm.Firstname.focus();
return (false);
}
if (theForm.Firstname.value.length < 3)
{
alert("Please enter at least 3 characters in the \"Firstname\" field.");
theForm.Firstname.focus();
return (false);
}
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var checkStr = theForm.Firstname.value;
var allValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only letter characters in the \"Firstname\" field.");
theForm.Firstname.focus();
return (false);
}
alertsay = "All Validations have succeeded. "
alertsay = alertsay +"Thank you for submittion"
alert(alertsay);
return (true);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
<script language="JavaScript1.2" src="js.js" type="text/javascript"></script>
</head>
<body>
<form action="validation.php" method="post" name="Form" id="Form" onsubmit="return Form_Validator(this)" >
<td><span class="redtext">*</span>indicates required fields. <br><br>
<!----indicates firstname field---->
class="redtext">*</span>First Name:</td>
<td width="285"><input name="Firstname" type="text" maxlength="50" class="input" id="Firstname"/></td>
<input name="submit1" type="submit" value="Submit" id="submit1 ">
</form>
</body>
</html>
Also, your HTML is not valid, as you are missing <table> tags and several elements are incomplete. May be these are just errors in pasting your example?