Solved

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

Posted on 2006-11-22
7
147 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

839 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