I create a regex to validate email address this is my regex:
@"^\w+(\.?[-+\w])*@\w+([-.]\w+)*\.[a-zA-Z]{2,80}$"
I would like that the max length of the mail address is 80 but with this regex i only limit the last part of the mail after the .
Now
aaa@aa.ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff is invalid
but
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@aa.ffffffff is valid
How can i do this?
You can validate within two steps.Just try it out.
Regex validCharsRegex = new Regex(@"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*" +
"@" + @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$");
Match match = validCharsRegex.Match(stringText.Trim());
if (match.Success && stringText.Length <=80 )
{
// You logic here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With