Solved

Can some one provide a function which checks if a string is an email address?

Posted on 2006-11-22
7
144 Views
Last Modified: 2006-12-08
Hi Experts,

Can some one provide a function which checks if a string is an email address?

Thanks
0
Comment
Question by:indyng
7 Comments
 
LVL 30

Expert Comment

by:third
ID: 17995411
try,

function isValidEmail(str){
  var reEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;  
  return reEmail.test(str);            
}
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17995844
 Since e-mail addresses can't start with a digit, I think this would be better:

  function validEmail( str ) {
    return /^[a-z_][a-z0-9_.-]*@[a-z0-9_.-]*(\.\w{2,7})+$/.test( str );
  }
0
 
LVL 1

Author Comment

by:indyng
ID: 17996397
How can I incorporate an error message? Thanks
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Accepted Solution

by:
third earned 200 total points
ID: 17996467

function isValidEmail(str){
  var reEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;  
 
  if(!reEmail.test(str)){
    alert('Invalid email!');
    return false;
  }
  else{
    return true;
  }
}
0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 150 total points
ID: 17996806
Hi,

try this..

if (document.formname.fieldname.value.length >0) {
       i=document.formname.fieldname.value.indexOf("@")
       j=document.formname.fieldname.value.indexOf(".",i)
       k=document.formname.fieldname.value.indexOf(",")
       kk=document.formname.fieldname.value.indexOf(" ")
       jj=document.formname.fieldname.value.lastIndexOf(".")+1
       len=document.formname.fieldname.value.length

       if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
       }
       else {
             alert("Please enter an exact email address.\n" +
            document.formname.fieldname.value + " is invalid.");
            return false;
       }

 }
 
http://www.pantz.org/scripting/javascript/formvalidation.shtml

R.K
0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 150 total points
ID: 17996814
 oops, forgot to add the case insensitivity

 function validEmail( str ) {
    return /^[a-z_][a-z0-9_.-]*@[a-z0-9_.-]*(\.\w{2,7})+$/.test( str );
  }
...

  Looking into "Mastering Regular Expressions" by Jeffrey Friedl, I find that something somewhat similar to the one supplied by third:

  /^\w[-.\w]*@[-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|gov|int|mil|net|org|biz|info|name|museum|coop|aero|[a-z][a-z]$/i

  Which is right for you?  How "accurate" do you want your match to be?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17996836
I "really did" put the trailing 'i' there... (sigh)

  return /^[a-z_][a-z0-9_.-]*@[a-z0-9_.-]*(\.\w{2,7})+$/i.test( str );
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now