Page 1 of 1
Would like to stop rest of form processing on history.back()
Posted: Fri Aug 13, 2004 2:00 pm
by voltrader
I have a script to validate a user's email address in my form on click of the "done" button. I also have a "back" button with code javascript:history.back();.
The problem I've encountered is that the email validation code won't let the back button complete its task.
In other words, on a click of the "back" button, the email validation script runs first and generates an alert, which prevents history.back() from executing.
Posted: Fri Aug 13, 2004 2:05 pm
by feyd
sounds like your back button is submitting... can you post your code?
Posted: Fri Aug 13, 2004 2:25 pm
by voltrader
Superfluous HTML extracted
Code: Select all
<script language='javascript'>
function validate_reg(form)
{
with(form)
{
// check e-mail
var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
var reg2 = /^.+\@(\ї?)їa-zA-Z0-9\-\.]+\.(їa-zA-Z]{2,3}|ї0-9]{1,3})(\]?)$/; // valid
var reg3 = /їa-zA-Z]/;
var str = contact_email.value;
if ((!reg1.test(str)) && reg2.test(str) && (reg3.test(str))) // if syntax is valid
{
// do nothing
}
else
{
alert("Please enter valid e-mail address" );
contact_email.focus();
return false;
}
if(contact_email1.value == "")
{
alert("Please enter confirmation e-mail address");
contact_email1.focus();
return false;
}
if(contact_email.value != contact_email1.value)
{
alert("E-mail addresses do not match");
contact_email1.focus();
return false;
}
}
}
</script>
<form enctype="multipart/form-data" action='cp.php' method=post name='cp' onsubmit="return validate_reg(document.cp);">
<!-- HTML GOES HERE -->
<tr>
<td>
<input type=text name=contact_email value=contact_email>
</td>
<td colspan=7>
<input type=text name=contact_email1 value=contact_email1>
</td>
</tr>
<table align=center>
<tr>
<td>
<input type=submit name=submit value="done">
<input type=submit name=button value="back" onclick="javascript:history.back(-1);">
</td>
</table>
</form>
Posted: Fri Aug 13, 2004 2:27 pm
by feyd
yep.. your back button submits.. change its type to button and it should be fine..
Posted: Fri Aug 13, 2004 2:56 pm
by voltrader
Thanks, yet again, feyd
