codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » ASP » Article

Email validation using Regular Expression

Article by:  Jeff Anderson  ( 1362 ) (8/3/2002)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Sponsored by: FindMyHosting - Web Hosting Search
Summary: Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Viewed: 315991 times Rating (111 votes): 
 4.6 out of 5
 Rate this Article  Read Comments  Post Comments



After several years of ASP development I still hadn't heard that regular expressions are possible in VBScript. But it seems I was wrong and in fact there is a built in object called RegExp.

We'll try to add a more indepth article about the use of Regular Expression in the near future, but for now, enjoy one of the simplest to use and most comprehensive email validation scripts you're likely to find!









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Email validation using Regular Expression'
Posted by :  Archive Import (gg) at 03:40 on Wednesday, October 09, 2002
Hi,
the expression is pretty good.
But i have a problem with restricting length of the email.
I've tried:
^([a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]){0,70}$

but it dos'nt work. I could probably do it by restricting the length of the address and domain separately, but i don't want to do that.
Is there a simpler way ?

thanks,
gg
Posted by :  Archive Import (123) at 04:58 on Saturday, December 07, 2002
thnx a lot for the code,,,

this site rocks!!!!
Posted by :  Archive Import (Mike) at 05:42 on Thursday, December 12, 2002
Many thanks, works well, and better than all other regular expressions I have so far tried.
Posted by :  Archive Import (llb) at 00:39 on Wednesday, January 08, 2003
Very limited email addresses. Does not allow email addresses with an IP address and port number.

Does not permit underscores (_) in the mail address (Which is valid)
Posted by :  Archive Import (Rob) at 07:20 on Tuesday, January 21, 2003
Thanks.

Disagree with llb as this *does* allow underscores eg my_name@this.my_domain.com but not leading or trailing '_'. Valid point about IP and port addresses being restricted tho.
Posted by :  Archive Import (alemo) at 10:16 on Thursday, January 23, 2003
check this: (is in Perl)

[\w\.\-]+\@[\w\.\-]+\.(com|edu|org|net|gov|mil|info){1}(\.(\w{2}))?$
Posted by :  Archive Import (Lusso) at 14:00 on Friday, January 24, 2003
I like short, clean code so I refactored the function as follows and it still seems to work ok. Did I take anything out important?


Function isValidEmail(myEmail)

Dim regEx

Set regEx = New RegExp
regEx.IgnoreCase = False
regEx.Pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"

isValidEmail = regEx.Test(myEmail)

End Function
Posted by :  Archive Import (Shefali) at 05:38 on Wednesday, January 29, 2003
Really neat, but do you have a regular expression that allows more than one email??? as in

1. panchals@vsnl.com
2. panchals@vsnl.com; panchals@vsnl.net

this way, either one or more emails can be added....

Thanks...
Posted by :  Archive Import (Zack) at 17:14 on Tuesday, February 18, 2003
I've been messing around with a regular expression for a list of emails (separated by ';'). This seems to work okay. Just remember to trim out any extra spaces and to remove a possible trailing ';'.


^(([\w-]+@[\w-]+\.(com|net|org|edu|mil)(\s*$|(\s*;\s*)))+)$
Posted by :  Archive Import (JB) at 12:35 on Tuesday, March 11, 2003
That email RegExp is great if your email isn't:

1@test.com

Keep on trying. You may come up with a perfect one in a couple hundred years :)
Posted by :  Archive Import (mario) at 15:47 on Saturday, March 15, 2003
this seams to solve some of the problems for ppl with emails such as 1@test.com
am i missing something?

regEx.Pattern = "^[a-zA-Z0-9]*[\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
Posted by :  Archive Import (James Laugesen) at 13:19 on Sunday, March 16, 2003
_example@example.com is a valid address.
All alphanumeric characters are valid for free text of an email address. Meaning the user name and domain name.

Here is mine.
/^[\w][\w\.-]*[\w]@[\w][\w\.-]*[\w]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/

I first split the input string into an array by ; then loop through validating, building an array of indexes of invalid emails.

I've never delt with IP emails, but I recon it'd be best to check for an IP pattern and execute a different expression.
Posted by :  Archive Import (tester) at 13:35 on Tuesday, April 08, 2003
i like this regular expression stuff. cool
Posted by :  Archive Import (Richard Marr) at 07:35 on Wednesday, April 09, 2003
I don't know if addresses that take the following form are valid:

user..name@domain.com

James's expression lets these through, but the only way I can think to exclude these effects how many dots you're allowed. i.e.

^[\w]*[\w\.-][\w]*@[\w]*[\w\.-][\w]*\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$

This also fails with short addresses such as 1@test.com.

I'm starting to think it might not be possible to perfectly validate all address forms with a single pattern.

I'm a complete RegExp newbie, so tell me if I'm talking rubbish :)

Rich
Posted by :  Archive Import (JohnnyM) at 12:05 on Friday, April 11, 2003
Here is an idea I came up with to skip multi dots. Your regex needs to support extended options. This is an Extention of James Laugesen's post. The prohibits periods that are followed by 1 or more extra periods.

/
^[\w]
( [\w-] | \. (?! \.+?) )*
[\w]
@
[\w]
( [\w-] | \. (?! \.+?) )*
[\w]
\. (?! \.+?)
[a-zA-Z]
( [\w-] | \. (?! \.+?) )*
[a-zA-Z]$
/x
Posted by :  Archive Import (rachits) at 23:32 on Thursday, April 17, 2003
Most email validators that arnen't at least a page long and are not complicated are not RFC822 compliant. Here is a C# shot at writing the email validator:

http://www.codeproject.com/csharp/RFC822Validator.asp

Most "valid" validators are based off of the ones in the Regex book written by Jeffrey Friedl.
Posted by :  Archive Import (Onigiri) at 10:49 on Thursday, May 22, 2003
man, you guys are forgetting that you _can_ run into an email address that is in a tld.. example of a valid one: n@ai
so you _cant_ force a . to be present...
Posted by :  Archive Import (Erestar) at 04:38 on Friday, May 23, 2003
Check this out...

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
Posted by :  Archive Import (Janine) at 11:11 on Monday, July 28, 2003
Thanks for this - this has really helped me as I was battling to do the validation correctly in asp using regexp
Posted by :  Archive Import (alex bates) at 09:40 on Friday, August 08, 2003
thanks for the help / site......... couldn't of managed with out it
Posted by :  Archive Import (suji) at 05:16 on Thursday, September 11, 2003
i go through ur email validation code. But i want validation of email like
abc@xyz.co.in
can anybody help me?
Posted by :  Archive Import (Eric) at 10:11 on Friday, September 12, 2003
I have to agree with rachits' comment, posted on April 17. I did find a translation of Jeffrey Friedl's validation in the language I needed (in my case: PHP), and it's working great. I would recommend it to anyone trying to validate email addresses.

A one-line regex to validate emails is more likely to fail on one address or another. It can't be perfect.
Posted by :  acksc at 22:33 on Saturday, November 13, 2004
<asp:RegularExpressionValidator runat="server"
ControlToValidate="Email"
ValidationExpression="\w+\@\w+\.com"
ErrorMessage="That is not a valid email address" />
Posted by :  jas99 at 05:11 on Tuesday, March 15, 2005
Hi,
I want to validate an apostrophe in email id i.e neil'oconnor@test.com. How do I do that.
I'm using following regexp:
var rx=new RegExp("^[\\w'\.=-]+@[\\w\\.-]+\\.[a-z]{2,4}$");if(!rx.test(myV))addErr=true;

Plz help
Posted by :  Priya_India at 05:52 on Thursday, May 19, 2005
I am new to ASP. In this email validation function, what is 'RegExp'. Is this a keyword or user-built function?

Regards,
Priya
Posted by :  matpoubelle at 23:08 on Tuesday, November 14, 2006
here is a expressionn that I tink can detect all the regular e-mail

it's a corection of the original one
-I add the underscores (_)
-it's now accept 1@test.com
-but for user..name@domain.com I dont know if its valid

[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]

Posted by :  copperchair32 at 07:23 on Thursday, November 30, 2006
Hello,

I have been trying hard to find an email regular validation expression which will only allow email addresses which end in the same way. Meaning that after the @ it has to be the same. Therefore only people from the company (has the same ending to the email for emplyees) will be able to register with the site. How can i go about this. email addresses go like:

[b]a.a.surname@building.company.co.uk[/b]

Is there anybody who can help me?

[:(]


<Added>

a.a.surname@building.company.co.uk
Posted by :  arjun200220 at 04:30 on Monday, December 11, 2006
can u plz tell me the date validator expression
format req. is mm/dd/yyyy
Posted by :  RajTN at 00:33 on Friday, February 09, 2007
Great Help from Codetoad

This [\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z] works for me.

Can any one give the Multiple users validation. Zack posted multiple which accepts only limited domain.

I need a code which should accept any domain in otherwords I need a code without specifying any domain names.

For example the following are valid email.
1. 1@abc.co 2. 1@abc.co.au 3. 1@abc.co.au; 1@abc.co 1. _1@abc.co

raj
Posted by :  siddhunttf at 02:16 on Friday, March 09, 2007
thanks for such a better idea.
Posted by :  tuhin at 00:44 on Monday, March 31, 2008
I want to validate only the body of the email, it should meet the criteria:
The body should not allow '@' and '.' but should allow underscore '_' while validating the email address.

eg: allowed format: great_lake
not allowed format: great.com
or great@com
or great_lake.com
or great_lake@r.com

Can somebody provide me with the reg-ex for the above
Posted by :  jnichols at 10:52 on Thursday, May 08, 2008
This is what I use the regular expression allows apostrophe's, underscores, a-z, 0-9, periods and splits at the @ symbol and allows two periods after it i.e. tool'omally@tool.co.uk or dick_head@tool.com

//validate the email
function validate_email($email)
{
// regexp for a valid email address
$regexp = "^(['_a-z0-9-]+)(\.['_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,5})$";

return (eregi($regexp, $email) > 0);
}


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
ASP Format Date and Time Script
by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
ASP FilesystemObject
by Jeff Anderson
An introduction to the Filesystemobject
Email validation using Regular Expression
by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Creating a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Create an ASP SQL Stored Procedure
by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page.
Creating an SQL Trigger
by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server
ASP Shopping Cart
by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members
Concatenate strings in sql
by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases).
The asp:checkbox and asp:checkboxlist control
by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.
ASP OpenTextFile
by Jeff Anderson
An introduction to the OpenTextFile Method of the FileSystemObject








Recent Forum Threads
• Re: Perl Script - File Handling.
• Open a file from website
• Re: to open 5 terminals from one and also execute different commands on each terminal
• read a selected multiple line, those should match some of the values read in the files..
• Help me please
• Empty the contents of a file.
• Month Start, End dates
• show hide problem in dynamic table creation
• Re: ASP Sendmail has huge delay - ANYONE??


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008