[SOLVED] Would like to stop rest of form processing on histo

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Would like to stop rest of form processing on history.back()

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sounds like your back button is submitting... can you post your code?
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Post by voltrader »

Superfluous HTML extracted

Code: Select all

<script language='javascript'>
function validate_reg(form)
&#123;
	with(form)
	&#123;	
		//  check e-mail

		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		var reg2 = /^.+\@(\&#1111;?)&#1111;a-zA-Z0-9\-\.]+\.(&#1111;a-zA-Z]&#123;2,3&#125;|&#1111;0-9]&#123;1,3&#125;)(\]?)$/; // valid
		var reg3 = /&#1111;a-zA-Z]/;
		
		var str	= contact_email.value;

		if ((!reg1.test(str)) && reg2.test(str) && (reg3.test(str))) // if syntax is valid
		&#123;
			// do nothing		 
		&#125;
		else
		&#123;
			alert("Please enter valid e-mail address" );
			contact_email.focus();
			return false;
		&#125;

		if(contact_email1.value == "")
		&#123;
			alert("Please enter confirmation e-mail address");
			contact_email1.focus();
			return false;
		&#125;

		if(contact_email.value != contact_email1.value)
		&#123;
			alert("E-mail addresses do not match");
			contact_email1.focus();
			return false;
		&#125;

	
	&#125;
&#125;
</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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yep.. your back button submits.. change its type to button and it should be fine..
User avatar
voltrader
Forum Contributor
Posts: 223
Joined: Wed Jul 07, 2004 12:44 pm
Location: SF Bay Area

Post by voltrader »

Thanks, yet again, feyd :D
Post Reply