Click to See Complete Forum and Search --> : Variable Scope & Validation


amacfarl
10-02-2003, 03:07 PM
Hi

I would like to write some code that validates the form and if any fields are not correct, the same page is displayed with additional text next to the fields that are not correct.

here is my code. I have tried with a simple test. I think it has something to do with the scope of the variable. I have you got any idea? Any help would be much appreciated

When the submit button is pressed:
<script language="JavaScript" type="text/JavaScript">
<!--
var sum;
function formSubmit()
{
sum = 1;
}
//-->
</script>

and further down the page I have the following code:
<script language="JavaScript" type="text/JavaScript">
<!--
if (sum = = 1)
document.writeln("Test");
//-->
</script>

AdamBrill
10-02-2003, 03:24 PM
Take a look at this:<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function validate(){
if(/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/i.test(document.form1.email.value)){
return true;
}else{
document.getElementById('email1').innerHTML="E-mail is not valid.";
return false;
}
}
</script>
</head>
<body>
<form name="form1" action="formhandler.php" onsubmit="return validate();">
Enter your e-mail: <input type="text" name="email"><span style="color:red;" id="email1"></span><br>
<input type="submit" value="Submit!">
</form>
</body>
</html>And as a bonus, you get a great E-mail validator(compliments of Jeff Mott). ;)

amacfarl
10-02-2003, 03:30 PM
THANKS!!!

AdamBrill
10-02-2003, 04:55 PM
No problem. :)