2

I have the following function:

function checkEmails(newEmail){
 $('table td:nth-child(3)').each(function(){
 if ($(this).html() == newEmail)
 {
 alert('The email address "' + newEmail + '" is already in the list. Duplicates are not allowed.');
 toggleSpinner();
 return false;
 } 
 });
 return true;
} 

I'm calling it this way in my form submit handler:

if (!checkEmails($('input#email', $('#newForm')).val())) {
 return false;
}//I submit the form via ajax next....

I'm just checking to make sure that the email address the user's trying to submit isn't already in a table. It seems to work good, except in Firefox, it doesn't actually stop the ajax request from occurring. The alert box appears, telling me the user's already in the list, but after clicking ok, the form is submitted anyway. It works as I want it to in IE.

What am I doing wrong here?

asked Jan 21, 2010 at 21:24

3 Answers 3

5

it should probably be done like this:

function checkEmails(newEmail){
 var ret = true;
 $('table td:nth-child(3)').each(function(){
 if ($(this).html() == newEmail)
 {
 alert('The email address "' + newEmail + '" is already in the list. Duplicates are not allowed.');
 toggleSpinner();
 ret = false;
 } 
 });
 return ret;
} 

What it is doing is setting the return value to true before doing the each on the elements to, then if it finds any invalid email addresses it will set it to false. That is the value that will be returned from the function.

answered Jan 21, 2010 at 21:27
Sign up to request clarification or add additional context in comments.

Comments

2

the return false is inside the closure so it doesn't break out of the outer function

i.e. it returns false for the nested function and not for checkEmails

answered Jan 21, 2010 at 21:26

Comments

1

I think you want this (use a bigFatGlobal to store the return value):

function checkEmails(newEmail){
 var bigFatGlobal = true;
 $('table td:nth-child(3)').each(function(){
 if ($(this).html() == newEmail)
 {
 alert('The email address "' + newEmail + '" is already in the list. Duplicates are not allowed.');
 toggleSpinner();
 bigFatGlobal = false;
 } 
 });
 return bigFatGlobal;
}
answered Jan 21, 2010 at 21:29

2 Comments

thats not really global is it?
well it is not local to the functions called by jQuery.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.