3 Replies - 347 Views - Last Post: 15 July 2012 - 11:33 AM

#1 AOM_Set  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 151
  • Joined: 18-December 10

Not returning false

Posted 13 July 2012 - 03:25 PM

Hi. I have this code to validate the email of the user. However, if the user input a wrong format, I want it to not go on, but wait till the email is the correct format. If I click on submit, it just sends, even though I wrote return false. I have also tried just writing "return". Any suggestions?
function checkEmail(){
	var email = document.getElementById('email');
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) {
		alert('Email is not in correct format');
		document.infoForm.email.style.background = "pink";
		email.focus();
		return false;
	} else{
		document.infoForm.email.style.background = "white";
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Not returning false

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1107
  • View blog
  • Posts: 6,924
  • Joined: 07-September 06

Re: Not returning false

Posted 13 July 2012 - 03:58 PM

What does the HTML form look like? If you want the form to not submit if the email check is bad you should have something like this:

<form method="post" action="somepage.php" onsubmit=return checkEmail()">
...
</form>



And your javascript will need to have a return true for it to work:
function checkEmail(){
	var email = document.getElementById('email');
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) {
		alert('Email is not in correct format');
		document.infoForm.email.style.background = "pink";
		email.focus();
		return false;
	} else{ // NOTE - This else statement isn't technically needed, since the if statement returns
		document.infoForm.email.style.background = "white";
	}
	return true;
}



Another question: Are you getting the alert with an incorrect format? I believe that should display even if the form isn't listening to the return value (and block the page load until it is dismissed).

Hope that helps.
Was This Post Helpful? 2
  • +
  • -

#3 AOM_Set  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 151
  • Joined: 18-December 10

Re: Not returning false

Posted 14 July 2012 - 01:22 AM

View PostBetaWar, on 13 July 2012 - 03:58 PM, said:

What does the HTML form look like? If you want the form to not submit if the email check is bad you should have something like this:

<form method="post" action="somepage.php" onsubmit=return checkEmail()">
...
</form>


The form looks like this
<form method="post" name="mainForm" id="form1" action="CustomerData.php" >	


I have more methods that need to validate before the action is triggered. Do I have to do like this for them all:
<form method="post" action="somepage.php" onsubmit="return checkEmail(), return method2(), return method3"> //And so on?


Or maybe it is smarter to make a method that calls all the methods that need to be returned, and then have that returned in the form on submit?

The else statement is necassary, since the color doesn't change back to normal when the issue is corrected. Actully since the else statement is excecuted, this shows that the return false statement isn't executed for some reason.

Yes I am getting the alert with an incorrect mail after I click the button
Was This Post Helpful? 0
  • +
  • -

#4 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 125
  • View blog
  • Posts: 818
  • Joined: 25-October 08

Re: Not returning false

Posted 15 July 2012 - 11:33 AM

View PostAOM_Set, on 14 July 2012 - 02:22 AM, said:

Or maybe it is smarter to make a method that calls all the methods that need to be returned, and then have that returned in the form on submit?


Yes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1