0

I used the following pattern to validate my email field.

    return Regex.IsMatch(email,
                 @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                 @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
                 RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));

It uses the following reference: https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-verify-that-strings-are-in-valid-email-format

My requirement is to have maximum number of 64 characters for user part, and max length for whole email string is 254 characters. The pattern in the reference only allow max 134 characters. Can someone give clear explanation of the meaning for the pattern? What is the right pattern to achieve my goal?

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
user3097695
  • 1,152
  • 2
  • 16
  • 42
  • https://regex101.com/ and https://regexper.com are handy for deciphering regular expressions and describing what each part does. – Rufus L May 31 '19 at 21:59
  • 1
    `...) && email.Length < 255 && email.Split('@')[0].Length < 65;` – Rufus L May 31 '19 at 22:03

3 Answers3

2

The code you cited is over-engineered, all you need to verify an email is to check for an at symbol and for a dot. If you need anything more precise, you are probably at a point where you actually need to email the recipient and ask for their confirmation that they hold the email, something that is simpler than a complex regex, and which provides much more precision.

Such a regex would simply be:

.+@.+\..+

Commentated below

.+      At least one of any character
@       The at symbol
.+      At least one character
\.      The . symbol
.+      At least one character 

Of course this means that some emails might be accepted as false positives, like tomas@company.c when the user intended tomas@company.com , but even if you design the most robust of regexes, one that checks against a list of accepted TLDs, you will never catch tomas@company.co, and you might insert positive falses like tomas@company.blockchain when a new TLD is released and your code isn't updated.

So just keep it simple.

TZubiri
  • 886
  • 11
  • 30
1

If you wanted to avoid using regex (which is, in my opinion, difficult to decipher), you could use the .Split() method on the email string using the "@" symbol as your delimiter. Then, you can check the string lengths of the two components from there.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
samyap
  • 131
  • 1
  • 7
1

Several years back, I wrote an email validation attribute in C# that should recognize most of that subset of syntactically valid email addresses that have the form local-part@domain — I say "most" because I didn't bother to try do deal with things like punycode, IPv4 address literals (dotted quads), or IPv6 address literals.

I'm sure there's lots of other edge cases I missed as well. But it worked well enough for our purposes at the time.

Use it in good health: C# Email Address validation

Before you go down the road of writing you own, you might want to read through the multiple relevant RFCs and try to understand the vagaries of what constitutes a "valid" email address (it's not what you think), and (2) stop trying to validate an RFC 822 email address. About the only way to "validate" an email address is to send mail to it and see if it bounces or not. Which doesn't mean that anybody is home at that address, or that that mailbox won't disappear next week.

Jeffrey Friedl's book Mastering Regular Expressions has a [more-or-less?] complete regular expression to match syntactically valid email addresses. It's 6,598 characters long.

Did you know that postmaster@. is a legal email address? It theoretically gets you to the postmaster of the root DNS server.

Or that [theoretically] "bang path" email addresses like MyDepartmentServer!MainServer!BigRouter!TheirDepartmentServer!SpecificServer!jsmith are valid. Here you define the actual path through the network that the email should take. Helps if you know the network topology involved.

Community
  • 1
  • 1
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135