...

View Full Version : form validation works in IE, not in Firefox



vinaip
02-07-2007, 01:39 AM
Hi,

I am validating form input to see if there are any blanks, and it works fine in IE, but not in Firefox. In Firefox, it gives the error "Fill in First Name", but then goes ahead to show the "Thank You" page. It should actually focus on the field and stay there.

Here's my code. You can see it in IE or firefox at http://www.brandrich.com/formval.html
(http://www.brandrich.com/formval.html)

I have posted it below also. Any help is appreciated to find the fix for Firefox.

Thanks a lot.

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
function validate(frm) {
//
// Customised Form Validation script - Vinai Prakash, Brandrich.com
//
// if (frm.givenname.value.length == 0)
if (document.forms[0].elements[0].value.length == 0)
{
alert("Please enter your First Name.");
frm.document.forms[0].elements[0].focus();
return false;
}
else if (document.forms[0].elements[1].value.length == 0)
{
alert("Please enter your Last Name.");
frm.document.forms[0].elements[1].focus();
return false;
}
else if (document.forms[0].elements[2].value.length == 0)
{
alert("Please enter the Email address.");
frm.document.forms[0].elements[2].focus();
return false;
}
//
// Now check the Email field for the "@" symbol
//
else if (document.forms[0].elements[2].value.indexOf("@") == -1)
{
alert("Please enter a valid e-mail address.");
frm.document.forms[0].elements[2].focus();
return false;
}
else {document.forms[0].elements[3].value = document.forms[0].elements[2].value;
return true;
};
}
//-->
</script>

</head>
<body>

<div id="myform" align="right">
<h4 class="sidebar-title">Get Blog Updates by Email</h4><br>
<form method="POST" action="http://www.brandrich.com/cgi-bin/form.pl" NAME="MyForm" onSubmit="return validate(MyForm)">
First Name: <input type="text" id="fname" name="first name" value="" size="20"><br>
Last Name: <input type="text" id="lname" name="last name" value="" size="20"><br>
Email : <input type="text" name="Email" value="" size="20"><br>
<input type="hidden" name="email">
<input type="hidden" name="newsletter_name" value="Business Email Marketing Blog">
<input type="hidden" name="recipient" value="subscribe@brandrich.com">
<input type="hidden" name="Subject" value="Subscribe to Business Email Marketing Blog">
<input type="hidden" name="redirect" value="http://www.brandrich.com/thankyou.html">
<input type="submit" name="submit" value="Email Blog Updates">
</form>
</div>


</body>
</html>

Arty Effem
02-07-2007, 02:10 AM
Hi,

I am validating form input to see if there are any blanks, and it works fine in IE, but not in Firefox. In Firefox, it gives the error "Fill in First Name", but then goes ahead to show the "Thank You" page. It should actually focus on the field and stay there.
The error is plainly indicated in the JS console. Your referencing scheme is not consistent. You're passing the name of the form to the validate function, which tries to reference it:
frm.document.forms[0].elements[0].focus();when it should be:
frm.document[frm].elements[0].focus();
That said, it would be better to pass a reference to the form
onSubmit="return validate(this)">then in the function, refer to the elements: frm.elements[...]

vinaip
02-07-2007, 02:23 AM
Thanks for the reaply Arty. I tried as per your suggestions, and created
http://www.brandrich.com/formval3.html

but now it does not work in IE or Firefox.

My earlier version at http://www.brandrich.com/formval.html is atleast working in IE properly and partially in FF.

Please advice if I goofed something in the formval3.html ?

vinaip
02-07-2007, 04:15 AM
Thanks Arty. I tried some more, and figured it out. Now it is working fine.

Incidently, i did not know what a JS console was. So I google it and found out how to use it. Thanks a lot!

Regards,
Vinai
http://www.brandrich.com



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum