/    Sign up×
Community /Pin to ProfileBookmark

Firefox ignores Javascript Validation, IE works fine

I am trying to validate the first name, last name, and email address field of a form.

Here is the javascript I used:

<HTML>
<HEAD>
<TITLE>Required Fields</TITLE>
<SCRIPT>
function validate() {
mfNv=form4.firstName.value;
if (mfNv==”) {
alert(‘Your first name is a required field. Please try again.’);
event.returnValue=false;
}
mlNv=form4.lastName.value;
if (mlNv==”) {
alert(‘Your last name is a required field. Please try again.’);
event.returnValue=false;
}

meAv=form4.emailAddress.value;
if (meAv==”) {
alert(‘Your Email Address is a required field. Please try again.’);
event.returnValue=false;
}
}
</SCRIPT>

If you would like to see the page, please visit [url]www.packagingleads.com/Subscribe.html[/url]

It works fine on IE, but on Firefox it doesn’t show up.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@mrhooJan 03.2008 — Your code is IE specific.

Only IE has a global event, the other browsers pass the event object as a parameter of the function.

event.returnValue is IE only.

It is also likely you will need to reference your elements differently-

form4.firstName.value may need to be

document.getElementsByName('firstName')[0]- if the field is the first element on the page that has 'firstName' set as its name.
Copy linkTweet thisAlerts:
@GZ2525authorJan 03.2008 — Thanks for the help. I tried the switch, but it didn't work.

Is there any code I can use that will work for both?
Copy linkTweet thisAlerts:
@GZ2525authorJan 03.2008 — upping for any help.
Copy linkTweet thisAlerts:
@GZ2525authorJan 03.2008 — Alright I changed my code. Now the problem with both right now is that it will popup the validation, but still go to my thank you page.

Any help would be very appreciated.

Here's my new code:

<SCRIPT>

function validate() {

if (form4.firstName.value == "")

{

alert("Your First Name is a required field. Please try again.");

return false;

}

if (form4.lastName.value == "")

{

alert("Your last name is a required field. Please try again.");

return false;

}

if (form4.emailAddress.value == "")

{

alert('Your Email Address is a required field. Please try again.');

return false;

}

}

</SCRIPT>
Copy linkTweet thisAlerts:
@stefanvverkJan 03.2008 — I thought I'd post my formValidation script while I'm waiting for a reply on my own topic here ?

Hope this helps, maybe it does, maybe it doesn't. Both IE and FireFox proof.

[CODE]
<script language="JavaScript">
function formValidation(formulier){
if (formulier.title.value.length == 0) {
alert("Fill in a title");
return false;
} else if (formulier.content.value.length == 0) {
alert("Fill in the content");
return false;
} else {
return true;
}
}
</script>
[/CODE]


And later on, inside the <body>
[CODE]
<form action=add_page.cgi?action=submit onSubmit="return formValidation(this)" method=POST name=formulier>
<input type=text name=title>
<textarea name=content rows=25 cols=90></textarea>
</form>
[/CODE]
Copy linkTweet thisAlerts:
@GZ2525authorJan 04.2008 — Thank You So Much! That helped me tremendously! It even kept the data in.
×

Success!

Help @GZ2525 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...