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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

email validate

Status
Not open for further replies.

GUJUm0deL

Programmer
Joined
Jan 16, 2001
Messages
3,676
Location
US
Hi folks, I have a field called EMAIL and I allow the user to enter multiple email address seperated by comma's, how can I use JS to validte the email syntax?

I use this as my email validation, and this works if the user enters ONE email. But what if they enter 'a@aol.com,b@bol.com,ca@aol.com'?

Code:
<SCRIPT LANGUAGE = "JavaScript">
function EmailValidate() {
   if (document.thisform.email.value.indexOf("@") == -1 || document.thisform.email.value == "") {
      alert("Please include a proper email address.");
      return false;
   }
}
</SCRIPT>

____________________________________
Just Imagine.
 
well first, you'll need to split the string into an array, based upon the comma. then you'll need to loop through the array and check each email address.

Code:
function EmailValidate() {
   var eString = document.forms['thisform'].['email'].value;

   if ( eString == "" ) {
      alert( "Please include a proper email address." );
      return false;
   } else {

      var eArray = eString.split(",");

      for ( var i = 0; i < eArray.length; i++ ) {
         if ( eArray[i].indexOf("@") == -1 ) {
            alert( "Please include a proper email address." );
            return false;
         }
      }
   }
   return true;
}



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Also, you might want to consider using an email validator with tighter restrictions. For instance the string "@" would pass your email validation routine, and this is clearly not a valid email address. Here's the regular expression I use for email validation:
Code:
function validateEmail(str) {
   return ((/\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,5}/).test(str))
}

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
kaht, any advantages/disadvantages to your function over this one?
Code:
function email(value) {
  var re = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
  return re.test(value);
}

I have not yet learned enough of regular expressions to read very deeply into them.


Google, you're my hero!
 
Thanks for the replies guys, but I decided to this server-side (using CF) instead. I figure server-side would more fool-proof (like in the event the user had JS disabled).

____________________________________
Just Imagine.
 
niteowl,

At first glance I would point out that email addresses can begin with numbers and underscores (yours will only accept alphabetical characters) - which prompted me to do a bit of digging for concrete rules.

I didn't spend a whole lot of time on it, but found this page which also covers a few other rules that my regexp didn't:


-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
You should always do server-side also but it is beneficial to do client-side validation as well so that the form cannot post unless all criteria are met first.
It's just a cleaner method of handling the data entry. If they disable javascript you would still test server-side and handle it from there.


Google, you're my hero!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top