Click here to Skip to main content
13,901,392 members
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 1 Jan 2012
Licenced CPOL

Regular expression to validate email on client side that allow whitespace in both end using RegularExpressionValidator control

, 1 Jan 2012
Rate this:
Please Sign up or sign in to vote.
RegEx for email validation that allow whitespace in both end
We can have lot of regular expressions here[^] to validate email from client side using ASP.NET RegularExpressionValidator control. Most of them plays well. One can choose any of them based on your requirement.

Anyway, please back to my point now. we know, trimming or ignoring whitespace is a pretty common requirement, so I would be very surprised if a validation couldn't handle it. Since ASP.NET RegularExpressionValidator control can't handle trim during validate so, we need modify the regex to allow whitespace in both end.

White space is represented in regex with \s. Zero or more white spaces would be \s*. Therefore, all you need to do is add \s* to the start and end of the expression, immediately inside the ^and$ start and end markers, like so:


Say we have

It can be modify by

Finally we can consider following code snippet
<asp:TextBox ID="txtEmail" runat="server"  CssClass="text"> </asp:TextBox>

<asp:RegularExpressionValidator ID="regValidator" runat="server" ErrorMessage="Email is not vaild." ForeColor="Red" ControlToValidate="txtEmail" ValidationExpression="^\s*(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*\s*$" ValidationGroup="register">Email is not vaild.</asp:RegularExpressionValidator>

Belief it, following code can be prefect to make tension-less validation if you have chance to validate email in server side rather then client side.
MailAddress mailAddress;

    mailAddress = new MailAddress(txtEmail.Text);
    var email = txtEmail.Text.Trim();
catch (Exception ex) 
    //Email considered invalid

Hope this is helpful, any alternative idea would be appreciated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shahdat Hosain
Software Developer (Senior)
United States United States
I have 5 year+ experience on ASP.NET, ASP.NET Web API, MVC, C#, SQL Server 2005, 2008, NopCommerce, E-commerce, , Windows Phone, LINQ2SQL, Entity Framework, LINQ2XML, ADO.NET, Telerik/RadControls, XHTML, XML, JavaScript, Jquery, Jquery Mobile, Ajax, HTML5 and CSS3.
I have significant knowledge on Java, JavaEE, EJB, JPA, JSF, Spring MVC, Hibernate, Design Pattern and problem domain analysis.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Ganesh Vellanki12-Nov-14 17:36
memberGanesh Vellanki12-Nov-14 17:36 
GeneralReason for my vote of 1 this is not correct regex for valida... Pin
Selvin1-Jan-12 23:26
memberSelvin1-Jan-12 23:26 
GeneralRe: Reason for my vote of 1this is not correct regex for valida... Pin
Shahdat Hosain6-Dec-13 12:43
memberShahdat Hosain6-Dec-13 12:43 
Hi Selvin, whats wrong you mean? can you explain little bit details in which case the regex does not work? thanks

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 1 Jan 2012
Article Copyright 2012 by Shahdat Hosain
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid