0

At work, our current ValidationExpression looks horrible and very confusing for me. We are using the WebForms <asp:RegularExpressionValidator> user control which looks like this:

<asp:RegularExpressionValidator ID="regEmail" runat="server" 
    ValidationGroup="EditEmails"
    Text="*" ErrorMessage="Invalid email address." 
    ControlToValidate="txtAdd" 
    Display="Dynamic" 
    ValidationExpression="^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$"/>

I need to somehow alter this to allow apostrophes ( ' ) inside an email because at the moment this expression is failing.

Example of an email that needs to pass this validation: Test.O'neill@example.co.uk

I am unsure what the expression does but I'm sure this could be made shorter (maybe not simpler but that does not matter as long as it works).

Anyone know of a better regular expression I could use which works against valid emails and takes this into consideration? Thank you!

EDIT: My question is different because the proposed duplicate question does not work for VB.Net RegularExpression Validator user control.

braX
  • 11,506
  • 5
  • 20
  • 33
Mayron
  • 2,146
  • 4
  • 25
  • 51
  • Possible duplicate of [How to validate an email address using a regular expression?](https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression) – Jason Mar 16 '18 at 16:06
  • https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression – Jason Mar 16 '18 at 16:06
  • Try [`^([a-z\d]+[_+.-])*[a-z\d']+@(\w+[.-])*\w{1,63}\.[a-z]+$`](https://regex101.com/r/CKffa0/1). It's **much** shorter than your current version and greatly simplifies it. – ctwheels Mar 16 '18 at 16:07
  • That possible duplication question refers to PHP and I think ASP.NET would vary slightly and not work, especially considering the length of it. – Mayron Mar 16 '18 at 16:08
  • @ctwheels Thank you. I'll test that out now :) I was really hoping to find a far more simplified one because the old looks like a terrible hack job. – Mayron Mar 16 '18 at 16:13
  • 1
    @Mayron no offense, but it is haha – ctwheels Mar 16 '18 at 16:14
  • @ctwheels no offense taken, it's not my code lol. I've only been asked to change it to provide apostrophe support. It hurts my eyes! – Mayron Mar 16 '18 at 16:15
  • @ctwheels Ah, damn. Doesn't seem to be working in vb.net :( – Mayron Mar 16 '18 at 16:26
  • @Mayron it should work in vb. Have you added the `i` flag (case-insensitive)? It's a regex option – ctwheels Mar 16 '18 at 16:27
  • @ctwheels Thanks! I followed this stackoverflow question to add that option: https://stackoverflow.com/questions/2641236/make-regular-expression-case-insensitive-in-asp-net-regularexpressionvalidator and it worked! – Mayron Mar 16 '18 at 16:32

1 Answers1

3

See regex in use here

^([a-z\d]+[_+.-])*[a-z\d']+@(\w+[.-])*\w{1,63}\.[a-z]+$
  • ^ Assert position at the start of the line
  • ([a-z\d]+[_+.-])* Capture the following any number of times
    • [a-z\d]+ Match any ASCII letter or digit one or more times (also matches uppercase variants with i flag enabled)
    • [_+.-] Match any character in the set
  • [a-z\d']+ Match any ASCII letter, digit, or apostrophe one or more times
  • @ Match this literally
  • (\w+[.-])* Capture the following any number of times
    • \w+ Match any word character one or more times
    • [.-] Match any character in the set
  • \w{1,63} Match any word character between one and 63 times
  • \. Match a literaly dot .
  • [a-z]+ Match any ASCII letter one or more times
  • $ Assert position at the end of the line

To implement the above pattern in a case-insensitive manner, add the RegexOptions.IgnoreCase flag. For more information see this post.

ctwheels
  • 21,901
  • 9
  • 42
  • 77