Click here to Skip to main content
12,625,402 members (35,395 online)
Rate this:
 
Please Sign up or sign in to vote.
Hello,

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

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 19-Sep-12 1:22am
Updated 19-Sep-12 2:52am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

if it is Windows Application
set property of Textbox
CharacterCasing = Lower
it will automatically convert uppercase letters to lowercase.

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
str.Contains("@")

Happy Coding!
:)
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

http://regexlib.com[^]

check this website for regular expressions



vote if it helps you
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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 :)
  Permalink  
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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 7 Dec 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100