Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple email address validation 2

Status
Not open for further replies.

mat41

Programmer
Mar 7, 2004
91
AU
Afternoon

I use the following code to determine 'is this a vlid email address'

Code:
email=document.userAdmin.uEmail.value;
var expression=/^([a-zA-Z0-9\-\._]+)@(([a-zA-Z0-9\-_]+\.)+)([a-z]{2,3})$/;
if(!(expression.test(email)) )
   {
      alert("Please enter a valid email addres to proceed");
      document.userAdmin.uEmail.select();
      document.userAdmin.uEmail.focus();
      return (false);
   }

Can anybody assit me writing a function which checks for more than one? These addresses will be entered into a text area seperated by a comer in the following format:

abc@somelocation.com, cde@someOtherPlace.com

In short my code does not allow for the validation of multiple addresses seperated by a commer in the one <input type="text"> tag

TYIA
 
what you need to do is break your single string into an array of strings by using the split() function. Then loop through each array element and validate each one as an email. Something like this:
Code:
email=document.userAdmin.uEmail.value;
var expression=/^([a-zA-Z0-9\-\._]+)@(([a-zA-Z0-9\-_]+\.)+)([a-z]{2,3})$/;
[red]emailArray = email.split(",")
for (var i=0, l=emailArray.length; i<l; i++) 
{[/red]
if(!(expression.test([red]emailArray[i].trim()[/red])) )
   {
      alert("Please enter a valid email address to proceed");
      document.userAdmin.uEmail.select();
      document.userAdmin.uEmail.focus();
      return (false);
   }
[red]}[/red]
Does that make sense?

Einstein47
“Evil abounds when good men do nothing.“ - Nelson R.
[&#91;]Starbase47.com]
 
Thank you for your prompt reply. I am an ASP guy whos JS is a bit rusty, yes I understand and appreciate your code changes. The form seems to post even if the syntax is not valid now, I suspect your for loop is not being entered into.

I fire the function in the form tag using:

Code:
onsubmit="return valUser();"

My complete function is:

function valUser()
{
   if (document.userAdmin.uEmail.value == "")
   {
      alert("You populate the email field to proceed.");
      document.userAdmin.uEmail.focus();
      return (false);
   }
   email=document.userAdmin.uEmail.value;
   var expression=/^([a-zA-Z0-9\-\._]+)@(([a-zA-Z0-9\-_]+\.)+)([a-z]{2,3})$/;
   emailArray = email.split(",")
   for (var i=0, l=emailArray.length; i<l; i++) 
   {
      if(!(expression.test(emailArray[i].trim())) )
      {
         alert("Please enter a valid email address to proceed");
         document.userAdmin.uEmail.select();
         document.userAdmin.uEmail.focus();
         return (false);
      }
   }
   return(true)
}

This one works:

Code:
if (document.userAdmin.uEmail.value == "")

however your modified version of my original does not, it posts. Any ideas?
 
further testing tells me the loop is being entered however its only looping once then submitting
 
Hi

Do you have the [tt]String.trim()[/tt] method defined ? If not, add this somewhere before the loop :
Code:
String.prototype.trim=function() { return this.replace(/^ +| +$/,'g') }

Feherke.
 
[blue]feherke[/blue] - I knew that I was putting my foot in my mouth - I use trim all the time, but I forgot that I had it defined in a global .js file. Thanks for bailing me out.

[blue]mat41[/blue] - instead of returning false from inside the loop - it would be better to have a flag initial set to true and then if one of the emails fails trip the flag and break out of the loop. Something like this:
Code:
[red]String.prototype.trim=function() { return this.replace(/^ +| +$/,'g') }[/red]

function valUser()
{
   if (document.userAdmin.uEmail.value == "")
   {
      alert("You populate the email field to proceed.");
      document.userAdmin.uEmail.focus();
      return (false);
   }
   email=document.userAdmin.uEmail.value;
   var expression=/^([a-zA-Z0-9\-\._]+)@(([a-zA-Z0-9\-_]+\.)+)([a-z]{2,3})$/;
   emailArray = email.split(",")
   [red]var submitOk = true[/red]
   for (var i=0, l=emailArray.length; i<l; i++)
   {
      if([red]submitOk &&[/red] !(expression.test(emailArray[i].trim())) )
      {
         alert("Please enter a valid email address to proceed");
         document.userAdmin.uEmail.select();
         document.userAdmin.uEmail.focus();
         [red]submitOk = false;[/red]
      }
   }
   return([red]submitOk[/red])
}

Now - THAT - should work.

Einstein47
“Evil abounds when good men do nothing.“ - Nelson R.
[&#91;]Starbase47.com]
 
Wow that works great!!!

Thankyou very much. i dont fully understand:

String.prototype.trim=function() { return this.replace(/^ +| +$/,'g') }

What exactly does this do?

TYIA
 
With JavaScript you can add your own functions to existing objects (the example here is the String object). So you are saying for any String on this page, create a new function called "trim()". And then you define your new function - in this case "trim" - to do something.

The line inside the trim function:
Code:
return this.replace(/^ +| +$/,'g')
takes the contents of the variable, and removes any spaces at the beginning or at the end of the String, and then returns a new String (so it isn't really changing what you have, but returning a new String that has been modified).

I hope that helps. This should really be an FAQ.

Einstein47
“Evil abounds when good men do nothing.“ - Nelson R.
[&#91;]Starbase47.com]
 
Understood, thanking you very much! :eek:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top