?
Solved

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

Posted on 2006-11-22
7
Medium Priority
?
152 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Accepted Solution

by:
third earned 800 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 600 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 600 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

850 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