Click here to Skip to main content
Rate this: bad
good
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 0:22am
Edited 19-Sep-12 1: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!
Smile | :)
  Permalink  
v2
Comments
Sebastian T Xavier at 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 at 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 3

Hi everyone,
 
I see your regular expression knowledge is really low. Am sure that you copied it from someone else Smile | :) 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 Smile | :)
  Permalink  
Comments
Sebastian T Xavier at 19-Sep-12 8:19am
   
This is definitely NOT going to work , as I have already tried this.
CodeHawkz at 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
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  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid