Connecting Tech Pros Worldwide Help | Site Map

Form validation script works in IE but not Mozilla

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 07:02 PM
webbedfeet@hyperlink.co.za
Guest
 
Posts: n/a
Default Form validation script works in IE but not Mozilla

Hi
I hope someone can help me. I have a client side form validation script
which works perfectly in IE but clicking "Submit" in Mozilla does
nothing - the form won't submit.
Is there something I can use that will work for both browsers, or is
there something wrong with my code? I seem to think that the problem
lies with the "onclick" area of the form.
This is my code, such as it is...

<SCRIPT LANGUAGE="JavaScript">
function ValidateEntries() {

if (MyForm.InputName.value == "") {
alert ("\nPlease enter your first name.")
return false;
}

if (MyForm.InputSurname.value == "") {
alert ("\nPlease enter your Surname.")
return false;
}

if ((MyForm.InputEmail.value == "") ||
(MyForm.InputEmail.value.indexOf('@') == -1) ||
(MyForm.InputEmail.value.indexOf('.') == -1)) {
alert ("\nPlease enter your Email Address.")
return false;
}

if (MyForm.InputAddress.value == "") {
alert ("\nPlease enter your Address.")
return false;
}

if (MyForm.InputPostalcode.value == "") {
alert ("\nPlease enter your Postalcode.")
return false;
}

if (MyForm.InputTelephoneNumber.value == "") {
alert ("\nPlease enter your Telephone Number.")
return false;
}

if (MyForm.InputComments.value == "") {
alert ("\nPlease type your Comments.")
return false;
}
else return document.MyForm.submit();
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1"></head>

<body bgcolor="#CCCCCC" leftmargin="5" topmargin="0">
<Form name="MyForm" action="sendmail.asp" method="post">

<table width="534" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"><font size="2" face="Arial, Helvetica,
sans-serif">Name
: </font></td>
<td valign="top"><input type="text" name="InputName"></td>
</tr>
<tr>
<td valign="top"><font size="2" face="Arial, Helvetica,
sans-serif">Surname
: </font></td>
<td valign="top"><input type="text" name="InputSurname"></td>
</tr>
<tr>
<td valign="top"><font size="2" face="Arial, Helvetica,
sans-serif">e-mail
Address: </font></td>
<td valign="top"><input type="text" name="InputEmail"></td>
</tr>
<tr>
<td valign="top"><font size="2" face="Arial, Helvetica,
sans-serif">Physical
Address : </font></td>
<td valign="top"><textarea name="InputAddress" rows="5"
cols="30"></textarea></td>
</tr>
<tr>
<td valign="top"><font size="2" face="Arial, Helvetica,
sans-serif">Postal
code : </font></td>
<td valign="top"><input type="text" name="InputPostalcode"></td>
</tr>
<tr>
<td valign="top"><font size="2" face="Arial, Helvetica,
sans-serif">Telephone
Number : </font></td>
<td valign="top"><input type="text"
name="InputTelephoneNumber"></td>
</tr>
<tr>
<td valign="top"><font size="2" face="Arial, Helvetica,
sans-serif">Message
: </font></td>
<td valign="top"><textarea name="InputComments" rows="5"
cols="30"></textarea></td>
</tr>
<tr>
<td colspan="2" valign="top"> <input type="button" value="Submit"
onclick="ValidateEntries()">
</td>
</tr>
</table>
</Form>
</body>

If anyone could help me, I would seriously reconsider dashing my
forehead against the desk
</thanks>


  #2  
Old July 23rd, 2005, 07:02 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: Form validation script works in IE but not Mozilla

webbedfeet@hyperlink.co.za writes:
[color=blue]
> I have a client side form validation script which works perfectly in
> IE but clicking "Submit" in Mozilla does nothing - the form won't
> submit.[/color]

Then you are doing something wrong. It should submit, just without
the validation, otherwise it won't work if Javascript is turned off.
[color=blue]
> Is there something I can use that will work for both browsers, or is
> there something wrong with my code?[/color]

It's your code.
[color=blue]
> <SCRIPT LANGUAGE="JavaScript">[/color]

Should be
<script type="text/javascript">
The type attribute is required by HTML, and is always sufficient.
[color=blue]
> function ValidateEntries() {
>
> if (MyForm.InputName.value == "") {[/color]

You have not declared a variable called MyForm. You assume that your
browser will create a global variable for your named form. Not all
browsers do that (IE does, Mozilla doesn't).

When referring to a form called "MyForm", always use:
document.forms['MyForm']
To save typing, you can assign the form reference to a variable:

var myForm = document.forms['MyForm'];

Then do
if (myForm.elements['InputName'].value == "") {

Ditto for the remaining tests.
(You could collect all the errors in one go, instead of only
showing the first one. That would speed up the user)

....[color=blue]
> else return document.MyForm.submit();[/color]

There should be no need for this, just return true, if you
make your page to work without javascript enabled.
[color=blue]
> }
> </script>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">[/color]

ICK. There should not be anything before the DOCTYPE declaration
in an HTML page. In particular, script elements are only valid
inside the HEAD and BODY elements. Please make your HTML validate!
[color=blue]
> <Form name="MyForm" action="sendmail.asp" method="post">[/color]
....[color=blue]
> <input type="button" value="Submit" onclick="ValidateEntries()">[/color]

This only works if Javascript is enabled. On the other hand, pressing
return in one of the text fields might submit the form without any
validation. A better approach is:

---
<form ... onsubmit="return ValidateEntries();">
...
<input type="submit" value="Submit">
---

Good luck.
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #3  
Old July 23rd, 2005, 07:09 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: Form validation script works in IE but not Mozilla

Lasse Reichstein Nielsen wrote:
[color=blue]
> To save typing[...][/color]

and increase efficiency,
[color=blue]
> you can assign the form reference to a variable: [...][/color]


PointedEars
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.