Checking the email address, again

Looking at the posts here for validating an email address, I am looking much more liberally about the client side testing that I am doing.

The closest I've seen so far:

^ ([\ w - \.] +) @ ((\ [[0-9] {1,3} \. [0-9] {1,3} \. [0-9] {1,3} \.) | (([\ w -] + \.) +))
([a-zA-Z] {2,4} | [0โ€“9] {1,3}) (\]?) $

This will not match this # strnage@foo.com which is valid according to the RFC

  • English upper and lower case letters (az, AZ)
  • Digits 0 to 9
  • Characters! # $% and '* + - / =? ^ _ `{| } ~
  • The character. (point, period, full stop) provided that it is not the first or last character and also provides that it does not appear two or more times in a row.

I need a pretty simple match:

  • Doesn't start with.
  • Any character allowed before @
  • Any character allowed after @
  • No consistent. or @allowed
  • Part after the last one. (tld) should be [a-z0-9-]

I will use \ i to make the search case insensitive. Consecutive characters are where I hung up.

+2


source to share


7 replies


If you want to conform to the official standard, you can use

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

      



Thus, even when adhering to official standards, there are still trade-offs. Don't blindly copy regular expressions from online libraries or discussion forums. Always check them against your own data and your own applications.

+4


source


/^[^.].*@(?:[-a-z0-9]+\.)+[-a-z0-9]+$/

      



+1


source


function validator(email) {
   var bademail = false;
   bademail = (email.indexOf(".") == 0) ? true : bademail;
   bademail = (email.indexOf("..") != -1) ? true : bademail;
   bademail = (email.indexOf("@@") != -1) ? true : bademail;
   if(!bademail) {
      var tldTest = new RegExp("[a-z0-9-]");
      var lastperiodpos = email.lastIndexOf(".");
      var tldstr = email.slice(lastperiodpos + 1);
      bademail = (!(tldTest.test(tldstr))) ? true : bademail;
   } 
   return bademail;
}

      

0


source


It depends on who is using your applications. For internal applications, the username is often a valid email address. Most RFC-822 email specifications describe additional fields that may be present in an email address. For example, Allen Town is a pretty standard email address that you can enter into your favorite email client. However, for an app, you may want to include a name to the email address when sending email, and don't want this to be part of the users' address.

The most liberal way of verifying an email address is to simply try to send an email to any address the user gives. If they receive an email and can verify it, then that is a valid address.

0


source


Perl-ish RFC822 supports regex here

0


source


It was useful for me now.

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 

      

0


source


The excellent validation regex is probably hard to match, but I've been using this for a while:

/^([\w-\.\+])+\@([\w-]+\.)+([\w]{2,6})+$/

      

Just recently changed it to fit 6LD char TLD.

-1


source







All Articles