Regular Expressions are very much useful for validation checking. It’s not a new technology; it originated in the UNIX environment, and is commonly used with the Perl language. Regular expressions are, however, supported by a number of .NET classes in the namespace System.Text.RegularExpressions
string email = txtEmail.Text;
Regex regex = new Regex(@”^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$”);
Match match = regex.Match(email);
if (match.Success)
MessageBox.Show(email + ” Correct Format”);
else
MessageBox.Show(email + ” Incorrect Format”);
write the above code in button click. validation get worked.
i have checked the following email to validate:
Invalid: @majjf.com
Invalid: A@b@c@example.com
Invalid: Abc.example.com
Valid: j..s@proseware.com
Valid: j.@server1.proseware.com
Invalid: js*@proseware.com
Invalid: js@proseware..com
Valid: ma…ma@jjf.co
Valid: ma.@jjf.com
Invalid: ma@@jjf.com
Invalid: ma@jjf.
Invalid: ma@jjf..com
Invalid: ma@jjf.c
Invalid: ma_@jjf
Invalid: ma_@jjf.
Valid: ma_@jjf.com
Invalid: ——-
Valid: 12@hostname.com
Valid: d.j@server1.proseware.com
Valid: david.jones@proseware.com
Valid: j.s@server1.proseware.com
Invalid: j@proseware.com9
Valid: j_9@[129.126.118.1]
Valid: jones@ms1.proseware.com
Invalid: js#internal@proseware.com
Invalid: js@proseware.com9
Invalid: js@proseware.com9
Valid: m.a@hostname.co
Valid: m_a1a@hostname.com
Valid: ma.h.saraf.onemore@hostname.com.edu
Valid: ma@hostname.com
Invalid: ma@hostname.comcom
Invalid: MA@hostname.coMCom
Valid: ma12@hostname.com
Valid: ma-a.aa@hostname.com.edu
Valid: ma-a@hostname.com
Valid: ma-a@hostname.com.edu
Valid: ma-a@1hostname.com
Valid: ma.a@1hostname.com
Valid: ma@1hostname.com
———————————————————
Some Related Validation for your reference
- Only Number – Validation
Regex regex = new Regex(@”^[0-9]+$”);
Number include decimal point, negative value and comma separator,
Regex regex = new Regex(@”^[0-9,\.-]+$”);
- Only Alphabets
Regex regex = new Regex(@”^[a-z]+$”);
- Only AlphaNumberics
Regex regex = new Regex(@”^[0-9a-z]+$”);
in all above if you remove + then its valid for only single character. + is for multi character validation