Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limit max length of mail address with regex + c#

Tags:

c#

regex

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?

like image 298
user1428798 Avatar asked Nov 02 '22 11:11

user1428798


1 Answers

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
   }
like image 67
Thilina H Avatar answered Nov 15 '22 05:11

Thilina H