Fixed! The problem is with the line breaks generated inside the text area as "\n". Not visible but are there and don't pass the validation test. Solution was to get rid of them and also white spaces by cleaning the text area input before passing them to the validation function. I added this: var emails = $('#emailList').val().split(' ').join(''); // Gets rid of white spaces var emails = emails.replace(new RegExp( "\\n", "g" ),''); // Gets rid of line breaks
On Aug 12, 9:34 pm, Miguel <jarib...@gmail.com> wrote: > I am stuck on this seemingly simple validation loop. > I want to iterate through a list of comma separated emails entered in > a textarea input and check their validity before submitting the form. > For some reason even when I enter all valid emails every other email > doesn't pass the validation test! > > var okEmailArr = badEmailArr = new Array(); > var emails = $('#emailList').val(); // Get email list from text are > input > var email_arr = emails.split(','); // split email list into array > var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4}) > $/; > > // iterate thru email array and check validity against regular > expression > > $.each(email_arr, function(i, n){ > > if(reg.test(n)) { > okEmailArr[i] = n; > alert('ok! -> '+n); > }else{ > badEmailArr[i] = n; > alert('error! -> '+n); > } > > }) > > Please help I am stuck! > > Thanks!