2

I have to implement email verification such that Email addresses cannot start or end with a dot.

The code is as below:

function validateEmail(elementValue)
{
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

   return emailPattern.test(elementValue);
}
5
  • 8
    What's your question? Commented Oct 14, 2011 at 14:08
  • 3
    Don't write your own verification, a simple google search would be enough
    – stivlo
    Commented Oct 14, 2011 at 14:10
  • 4
    Why can't email addresses start with a dot? (And that expression will rule plenty of perfectly fine email addresses (including some of mine) as invalid).
    – Quentin
    Commented Oct 14, 2011 at 14:13
  • 1
    This is a FAQ. Read haacked.com/archive/2007/08/21/… and ex-parrot.com/pdw/Mail-RFC822-Address.html or if you think it looks too long, Just Don't Do It.
    – tripleee
    Commented Oct 14, 2011 at 14:21
  • 1
    There is no foolproof way of checking the validity of an email address. In addition to matching against the regular expressions, you must also trim the string and check the min/max lengths.
    – RHT
    Commented Oct 14, 2011 at 14:32

6 Answers 6

2

The simplest JavaScript-compatible change you could make to ensure that it does not start with a period/dot/decimal-point would be to use a negative lookahead like so: (?!\.) at the beginning of the expression:

function validateEmail(elementValue)
{
   var emailPattern = /^(?!\.)[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

   return emailPattern.test(elementValue);
}

There are plenty of cases this does not handle, and depending upon your reasons for this need, it might be one of thousands of things that go into creating a perfect RFC-2822 compliant email address (which I don't believe actually exists in any commercially viable system or "in the wild") - that you don't really need to worry about.

you could also simplify it further by making it case-insensitive:

/(?!\.)[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}/i

or even better...

/(?!\.)[\w.-]+@[a-z0-9.-]+\.[a-z]{2,4}/i

and you might want to consider (if you haven't already) the .travel and .museum TLDs that would be invalidated by your {2,4} length limitation

1

var emailAddressPattern = /(((\s*([^\x00-\x1F\x7F()<>[]:;@\,."\s]+(.[^\x00-\x1F\x7F()<>[]:;@\,."\s]+))\s)|(\s*"(([^\"])|(\([^\x0A\x0D])))+"\s*))\@((\s*([^\x00-\x1F\x7F()<>[]:;@\,."\s]+(.[^\x00-\x1F\x7F()<>[]:;@\,."\s]+))\s)|(\s*[(\s*(([^[]\])|(\([^\x0A\x0D])))+)\s]\s*)))/;

0

You have to define that in your regexp:

var emailPattern = /^[a-z0-9_-][a-z0-9._-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i;

[a-z0-9_-] means: 1 character of the set a-z0-9_- (so not a dot). I also changed + to *, because the [a-z0-9_-] now already accounts for at least one character.

Your regexp already says that it should not end with a dot, since you require 2 to 4 letters at the end.

With the i flag after the last /, you can eliminate a-zA-Z and only use a-z (or A-Z) since it means "case-insensitive".

3
  • 1
    Don't you need to escape the dot? [^\.] Commented Oct 14, 2011 at 14:16
  • 1
    @MrMisterMan: No, that's not necessary inside a character class.
    – pimvdb
    Commented Oct 14, 2011 at 14:17
  • 1
    uhhh - this also allows someone to start their email address with an @ symbol... ??!?! Commented Oct 14, 2011 at 14:19
0

Please be aware that validating email addresses is notoriously hard. You're almost certain to get it wrong -- i.e. to be too strict -- if you try to do it yourself. Have you read the RFC?

There's a lot more information here:

That page compares a number of regular expressions against several inputs, including two that directly address your requirements:

Lots more reading:

0

The RFC for email addresses gives a lot of room for what an email address could be including the characters being used, the length of email address. For practical purpose in almost every application:

  1. The length of the column is fixed for email field
  2. The character set is fixed on the database side

So for all real applications using a standard regular expression to validate email address should be fine unless you are government where you have legal obligations to support valid email address.

Any real world user using out of the world, RFC compliant email address will face the challenge enough number of times and will change it.

0
<input type='text' id='emailInput'/>
<input type='submit' name='submit' onclick='Javascript:validateEmail();'/>

<script language="javascript">

function validateEmail() {

    var email = document.getElementById('emailInput');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email.value)) {
    alert('Please provide a valid email address');
    email.focus;
    return false;
 }
}</script>

Not the answer you're looking for? Browse other questions tagged or ask your own question.