Forum Moderators: open

Message Too Old, No Replies

Javascript not validating

         

obeamma

3:00 pm on Dec 24, 2007 (gmt 0)

10+ Year Member



Hello

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>

penders

6:54 pm on Dec 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If your // JavaScript Document is indeed an external JS file (is this your "js.js" file?) then it shouldn't contain surrounding <script> tags as you have posted above? This could well cause nothing to happen, or a JavaScript error if you have JS error alerts enabled.

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?

obeamma

3:11 pm on Dec 25, 2007 (gmt 0)

10+ Year Member



thanks..I did paste the html document wrong