12

We are using the following to do an email validation in ASP.NET:

\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

How can this be modified to ignore leading and trailing spaces?

The actual trimming we handle in code on postback but the validator is triggering as being invalid if the user has an extra space often due to copying and paste.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
schooner
  • 3,047
  • 8
  • 30
  • 39
  • You should read this: http://mail.python.org/pipermail/python-list/2006-September/404146.html – Gumbo Feb 21 '09 at 18:36
  • Sending a test message is not an option. It needs to be validated as best as possible on the form prior to submission. – schooner Feb 21 '09 at 18:39

5 Answers5

19

You can place \s* before and after your pattern and it should work properly.

localshred
  • 2,244
  • 1
  • 21
  • 33
  • 1
    your suggestion works: `\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*` – Vaibhav Garg Jun 17 '13 at 08:20
  • I know this is pretty old, but I think its still very relevant today. If the goal is to make a regex just for raw user inputted email then I think this is the correct answer. However if you want a regex for a valid email this is wrong. On large apps you will want to reuse the same regex in many layers, in small apps this would work fine, but you should know it doesn't scale. – Chris Stephens Aug 21 '13 at 15:22
7

Group what you want into a named capture and then allow spaces before and after the capture

\s*(?<email>\w+([-+.']\w+)@\w+([-.]\w+).\w+([-.]\w+)*)\s*
Jeff Moser
  • 19,727
  • 6
  • 65
  • 85
4

Just do the trim before you pass it to the validator.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin
  • 7,162
  • 11
  • 46
  • 70
  • It is an asp.net validator linked to a textbox, so not sure that is possible by default. I'd prefer to have the regex handle it if possible. – schooner Feb 21 '09 at 18:31
  • 1
    Why? The spaces are *not* part of a valid email, so the regex shouldn't see them. Trim the value with an onchange/onsubmit action. – Peter Boughton Feb 23 '09 at 08:50
  • I prefer this more than doing it in a regex. " myemail@email.com " isn't a valid email. The extra spaces are more related to the source of data. Like you wouldn't validate an email in the DB with the same email regex that accepts spaces. If you get the email from a query string you wouldn't use a regex that allows encoded stuff, You would decode it first then validate. If they are using MVC validators then they are at the mercy of MS engineering. So you might not have a choice. – Chris Stephens Aug 21 '13 at 15:17
1

In case of ASP.NET the following works:

<asp:TextBox runat="server" ID="EmailAddress" />
<asp:RegularExpressionValidator ValidationExpression="\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*" runat="server" ControlToValidate="EmailAddress" Text="*" ErrorMessage="Email is not valid"  />

Allows a variety of emails like:

"some@one.com"
"  some@one.com  "
"so_me@one.co.uk"
" a.s-d__+f@asd.as.as "

etc

Make sure you strip the white spaces in your code like so:

VB Dim Email as string = EmailAddress.Text.Trim()

C# string Email = EmailAddress.Text.Trim();

Vaibhav Garg
  • 3,630
  • 3
  • 33
  • 55
0

I was going to "answer" this as a simple comment, but the real answer to your question is that there is no answer.

Why?

Because any email address with space or whitespaces before or after it, is an invalid email address.

You try and pass an email address with whitespace chars around it into an SMPTP control for example, and try and send it, it will fail!

All email addresses have to be validated from the point of view of this regex...

\w+([-+.']\w+)@\w+([-.]\w+).\w+([-.]\w+)*

or even this

/.+@.+/

Anything else is an invalid email address.

Really the way around it is to TRIM() the string before passing into an email control to use it.

Just warning you.

Fandango68
  • 4,461
  • 4
  • 39
  • 74