PDA

View Full Version : Works in IE but not Netscape


ozz
03-24-2003, 07:00 PM
Can one of the Gurus have a quick look at this please as the validation works for me in IE but not NS??




<html>

<head>
<title>New Page 1</title>

<script>

<!--
function validateform()
{
if(document.form1.name.value=="") {
window.alert ("Please Enter a Name!");
form1.name.focus();
return (false);
}
if(document.form1.dept.value=="") {
window.alert ("Please Enter your Department!");
form1.dept.focus();
return (false);
}

if (/\D/g.test(form1.idnum.value) || (form1.idnum.value == ''))
{
alert ("You have not entered your ID Number!");
form1.idnum.value = "";
form1.idnum.focus();
return false;
}

if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form1.email.value)){
return (true);
}
alert ("Missing or Invalid E-mail Address! Please re-enter.");
form1.email.focus();
return (false);
}

//-->
</script>
</head>

<body>

<form method="POST" name="form1" action="" onSubmit="return validateform( this )">
<p>NAME:<input type="text" name="name" size="20"></p>
<p>DEPT:<input type="text" name="dept" size="20"></p>
<p>ID NUM:<input type="text" name="idnum" size="20"></p>
<p>EMAIL:<input type="text" name="email" size="20"></p>

<p><input type="submit" value="Submit" name="sub1"><input type="reset" value="Reset" name="reset1"></p>
</form>

</body>

</html>

arnyinc
03-24-2003, 07:27 PM
Netscape is more picky about properly referencing objects. Anywhere that you have form1, it should be document.form1.
You also pass "this" to your function in the onsubmit, but don't use it.

<html>

<head>
<title>New Page 1</title>

<script>

<!--
function validateform(myform)
{
if(myform.name.value=="") {
alert ("Please Enter a Name!");
myform.name.focus();
return (false);
}
if(myform.dept.value=="") {
alert ("Please Enter your Department!");
myform.dept.focus();
return (false);
}

if (/\D/g.test(myform.idnum.value) || (myform.idnum.value == ''))
{
alert ("You have not entered your ID Number!");
myform.idnum.value = "";
myform.idnum.focus();
return false;
}

if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myform.email.value)){
return (true);
}
alert ("Missing or Invalid E-mail Address! Please re-enter.");
myform.email.focus();
return (false);
}

//-->
</script>
</head>

<body>

<form method="POST" name="form1" action="" onSubmit="return validateform( this )">
<p>NAME:<input type="text" name="name" size="20"></p>
<p>DEPT:<input type="text" name="dept" size="20"></p>
<p>ID NUM:<input type="text" name="idnum" size="20"></p>
<p>EMAIL:<input type="text" name="email" size="20"></p>

<p><input type="submit" value="Submit" name="sub1"><input type="reset" value="Reset" name="reset1"></p>
</form>

</body>

</html>

ozz
03-24-2003, 07:40 PM
Cheers, works a treat!! :o

Philip M
03-24-2003, 07:45 PM
Tested in MSIE, Netscape, Opera.

<html>

<head>
<title>New Page 1</title>

<script>

<!--
function validateform(which)
{
if(which.name.value=="") {
window.alert ("Please Enter a Name!");
which.name.focus();
return (false);
}
if(which.dept.value=="") {
window.alert ("Please Enter your Department!");
which.dept.focus();
return (false);
}

if (/\D/g.test(which.idnum.value)|| (which.idnum.value == ''))
{
alert ("You have not entered your ID Number!");
which.idnum.value = "";
which.idnum.focus();
return (false);
}

if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(which.email.value)){
return (true);
}
alert ("Missing or Invalid E-mail Address! Please re-enter.");
which.email.focus();
return (false);
}

//-->
</script>
</head>

<body>

<form method="POST" name="form1" action="" onSubmit="return validateform( this )">
<p>NAME:<input type="text" name="name" size="20"></p>
<p>DEPT:<input type="text" name="dept" size="20"></p>
<p>ID NUM:<input type="text" name="idnum" size="20"></p>
<p>EMAIL:<input type="text" name="email" size="20"></p>

<p><input type="submit" value="Submit" name="sub1"><input type="reset" value="Reset" name="reset1"></p>
</form>

</body>

</html>