Solved

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

Posted on 2006-11-22
7
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

696 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