Click here to Skip to main content
14,978,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have a regular expression to validate email address. Here it is....

C#
string pattern = "^[^@]+@([-\\w]+\\.)+[A-Za-z]{2,4}$";

System.Text.RegularExpressions.Match match = Regex.Match(txtEmailChecking.Text.Trim(), pattern, RegexOptions.None);
            if (match.Success)
                MessageBox.Show("Success", "Checking");
            else
                MessageBox.Show("Fail", "Checking");


But this expression will allow capital letters in email address. How can I restrict that?

Besides that , this expression allows more than one @. How can I fix this?

Thanks for your support

Regards
Sebastian
Posted
Updated 16-Apr-21 11:48am
v2

if it is Windows Application
set property of Textbox
CharacterCasing = Lower

it will automatically convert uppercase letters to lowercase.

C#
string pattern="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,3})$";


and verify it when textbox's Leave event fire. at a time when you are writing it will allow mistakes like '@' is two time but when you will leave textbox it will give error.

if you do not want '@' at time of keypress event then on key press event you should handle '@' keychar if it's used second time. you can check if '@' is already exist in string using below function
C#
str.Contains("@")


Happy Coding!
:)
   
v2
Comments
Sebastian T Xavier 19-Sep-12 7:51am
   
This is really informative; thanks... My +5 for it. I have another problem. My expression allows more than one @.
Aarti Meswania 19-Sep-12 8:05am
   
please check updated solution may it be useful for second problem
http://regexlib.com[^]

check this website for regular expressions



vote if it helps you
   
Hi everyone,

I see your regular expression knowledge is really low. Am sure that you copied it from someone else :) Not to worry, you can learn regular expressions really fast.

First things first, you don't normally validate for capitals in e-mail. Instead, what you do is, when you save it into your database, you case it to lowercase using string.ToLower()[^] method.

But in case, you really want to validate that with that expression of yours, all you have to do is remove A-Z from your expression. It would look like;
string pattern = "^[^@]+@([-\\w]+\\.)+[a-z]{2,4}$"


If you like to read about regular expressions more, I found these links to be really helpful. They explain things very clearly, makes it easy to understand.
Regular Expressions - User Guide[^]
jQuery RegEx Examples to use with .match()[^]
Regular Expression Basic Syntax Reference[^]

Hope this helps, regards :)
   
Comments
Sebastian T Xavier 19-Sep-12 8:19am
   
This is definitely NOT going to work , as I have already tried this.
CodeHawkz 19-Sep-12 11:10am
   
:D I'd bet on it that your regular expression is not validating capital letters. If you want some other error to be fixed let me know. Good luck
Regex : <pre>"^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$"
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900