event.preventDefault() doesn't work on mozilla
Emi
I'm having trouble getting a feature to work when using Google Chrome, but in Mozilla I'm having trouble:
The JavaScript code is:
<script type="text/javascript">
function gid(id) {
return document.getElementById(id);
}
function validate() {
var x = document.forms["frmSurvey"]["email"].value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if((gid('0_stars').checked==true) || (gid('1_stars').checked==true) || (gid('2_stars').checked==true) || (gid('3_stars').checked==true) || (gid('4_stars').checked==true)) {
if((gid('comment').value != '') && (gid('email').value != '') && (gid('name').value != '')) {
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
alert("Not a valid e-mail address. Please write a valid e-mail address!");
return false;
} else {
document.getElementById("frmSurvey").submit();
}
}
else {
alert('You haven`t completed the mandatory fields. Please make sure that you complete the form corectly! Thank you!');
return false;
}
}
else {
alert('You haven`t completed the mandatory fields. Please make sure that you complete the form corectly! Thank you!');
return false;
}
}
</script>
The form is:
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post" id="frmSurvey" onsubmit="event.preventDefault(); validate();">
...
...
...
</form>
Is there any way to replace this functionality with other methods? I've tried returning false; but it doesn't commit at all!
Rick Hitchcock
added return true
to the end validate
.
Then change:
onsubmit="event.preventDefault(); validate();"
... arrive:
onsubmit="return validate();"
This will cause the form to be submitted only after it has been validated.
edit
After doing this, you can remove this line from it validate()
:
document.getElementById("frmSurvey").submit();