/    Sign up×
Community /Pin to ProfileBookmark

javascript FF problem

I am using the following script to validate a form.

THee script is called using

<form name=”frmComments” action=”test.php” method=”post” onSubmit=”return validate(this)”>

It works fine in IE but does not work in FF. It pops the error but still submits the form.

Any help appreciated ..

thx

<script language=”JavaScript” type=”text/javascript”>
<!–
function validate(theForm)
{
var validity = true; // assume valid
var fname = frmComments.firstname.value;
var lname = frmComments.lastname.value;
var address = frmComments.address1.value;
var company = frmComments.company.value;
var city = frmComments.city.value;
var zip = frmComments.zip.value;

var phone=frmComments.phone.value;

fname = fname.replace(/^s*|s*$/g,””);
lname = lname.replace(/^s*|s*$/g,””);
address = address.replace(/^s*|s*$/g,””);
city = city.replace(/^s*|s*$/g,””);
zip = zip.replace(/^s*|s*$/g,””);

if(fname==” && validity == true)
{
alert(‘Your First Name is required. Please enter your First name!’);
validity = false;
frmComments.firstname.focus();
event.returnValue=false;
}

if(lname==” && validity == true)
{
alert(‘Your Last Name is required. Please enter your Last Name!’);
validity = false;
frmComments.lastname.focus();
event.returnValue=false;
}

if(address==” && validity == true)
{
alert(‘Your address is required. Please enter your address!’);
validity = false;
frmComments.address.focus();
event.returnValue=false;
}

if(city==” && validity == true)
{
alert(‘Your city is required. Please enter your city!’);
validity = false;
frmComments.city.focus();
event.returnValue=false;
}

if(zip==” && validity == true)
{
alert(‘Your zip/postal code is required. Please enter your zip/postal code!’);
validity = false;
frmComments.zip.focus();
event.returnValue=false;
}

if ((phone==null)||(phone==””) && validity == true)
{
alert(‘Please Enter your Phone Number!’);
frmComments.phone.focus();
validity = false;
return false
}

var pattern2 = /d{3}d{3}d{4}/;
if(!pattern2.test(frmComments.phone.value)&& validity == true)
{
alert(“Invalid Phone Number! Must be in form 999-999-9999. Please re-enter.”);
frmComments.phone.focus();
validity = false;
return false;
}

var pattern1 = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,4})+$/;
if (!pattern1.test(frmComments.email.value) && validity == true)
{
alert(“Invalid E-mail Address! Please re-enter.”)
frmComments.email.focus();
validity = false;
return false;
}

}
//–>
</script>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinFeb 02.2007 — you may have helped yourself here if you included what error you get. We could have solved it most likely from that!

//erlin!
Copy linkTweet thisAlerts:
@felgallFeb 02.2007 — event.returnValue=false;

is IE only since other browsers don't have an event object. Try replacing those lines with:

return false;
Copy linkTweet thisAlerts:
@swapdudeauthorFeb 02.2007 — Thanks. Works fine now.

event.returnValue=false;

is IE only since other browsers don't have an event object. Try replacing those lines with:

return false;[/QUOTE]
×

Success!

Help @swapdude 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...