Email verification in JavaScript
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);
}
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
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*)))/;
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".
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:
- Comparing E-mail Address Validating Regular Expressions
That page compares a number of regular expressions against several inputs, including two that directly address your requirements:
.local-starts-with-dot@sld.com
local-ends-with-dot.@sld.com
Lots more reading:
- http://www.regular-expressions.info/email.html
- http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx
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:
- The length of the column is fixed for email field
- 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.
<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>
精彩评论