Solved

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

Posted on 2006-11-22
7
148 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

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

Suggested Solutions

Title # Comments Views Activity
Google Tag Manager - Add Trigger Using Div class 22 61
Understanding UserAgent String 2 48
Read text on Table 7 29
How do I split a variable to newline 2 19
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

733 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