Paul Kahl
asked on
Email Address Validation in C# 2.0 (Studio 2005)
Greetings.
I am BRAND NEW to C#, and moreover, brand new to Visual Studio 05.
I have a simple page with a single email address field and a submit button.
I have an empty field validation on the page ("You must enter your Email Address"), but I would also like to add validation on the email address.
I have a code snippet from another topic here that had an email validation function, and I have a couple questions about it.
the code:
public static bool isEmail(string inputEmail)
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\ [[0-9]{1,3 }" +
@"\.[0-9]{1,3}\.[0-9]{1,3} \.)|(([a-z A-Z0-9\-]+ \" +
@".)+))([a-zA-Z]{2,4}|[0-9 ]{1,3})(\] ?)$";
Regex re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
return (true);
else
return (false);
}
the idea:
I'd like to alter this so it not only does the email address validation, but makes sure the email is in one of two domains: mydomain1.com, mydomain2.com.
I would also like to have this event fire onclick of the submit button.
So... the big "I'm with Stupid (^)" question: How do I do all that? :)
~ Paul K.
I am BRAND NEW to C#, and moreover, brand new to Visual Studio 05.
I have a simple page with a single email address field and a submit button.
I have an empty field validation on the page ("You must enter your Email Address"), but I would also like to add validation on the email address.
I have a code snippet from another topic here that had an email validation function, and I have a couple questions about it.
the code:
public static bool isEmail(string inputEmail)
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\
@"\.[0-9]{1,3}\.[0-9]{1,3}
@".)+))([a-zA-Z]{2,4}|[0-9
Regex re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
return (true);
else
return (false);
}
the idea:
I'd like to alter this so it not only does the email address validation, but makes sure the email is in one of two domains: mydomain1.com, mydomain2.com.
I would also like to have this event fire onclick of the submit button.
So... the big "I'm with Stupid (^)" question: How do I do all that? :)
~ Paul K.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the great explanation!
I'm not new to RegExp (been using JS and ASP regexp for years, but never heavily), so I understand exactly what you mean... But the clarification is also incredibly handy as I'm trying my best to focus on the C# aspects right now, and less on the "middle-man" things like the regular expressions.
The finished code winds up looking like so:
public static bool isEmail(string inputEmail)
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((d omain1\.co m)|(domain 2\.com))$" ;
Regex re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
return (true);
else
return (false);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!isEmail(this.txtEmailAdd ress.Text) )
lblErrorMessage.Text = this.txtEmailAddress.Text + " is not a valid email address.";
else
lblErrorMessage.Text = "Email Address is valid.";
}
Thanks tremendously once again!
I'm not new to RegExp (been using JS and ASP regexp for years, but never heavily), so I understand exactly what you mean... But the clarification is also incredibly handy as I'm trying my best to focus on the C# aspects right now, and less on the "middle-man" things like the regular expressions.
The finished code winds up looking like so:
public static bool isEmail(string inputEmail)
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((d
Regex re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
return (true);
else
return (false);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!isEmail(this.txtEmailAdd
lblErrorMessage.Text = this.txtEmailAddress.Text + " is not a valid email address.";
else
lblErrorMessage.Text = "Email Address is valid.";
}
Thanks tremendously once again!
My pleasure. Its worth looking up Expresso if you are going to use more regular expressions in .NET.
ASKER
I thikn I have it installed, actually. I used it quite frequently in Studio 03.
Perfectly poised for world domination then ;-)
The code that you have gives you a regular expression, which is a way of comparing text to patterns. I'm guessing you haven't done much regular expressions before either? They are a bit of a black art. The expression that you had in that code looked like it came straight out of Expresso, which is a great (free) tool for learning, developing and testing regular expressions, available at http://www.ultrapico.com/Expresso.htm.
The regular expression I have written above basically says:
() gives a "capture group" i.e. a section of the expression
[a-zA-Z0-9_\-\.] gives any of the characters a-z, A-Z, 0-9, _, - or .
\ escapes a character such as - or . which is otherwise a "special" regex character
+ gives 1 or more characters
That makes up the first part, which basically gives you the first part of an email address.
Then there's the @, which is self-explanatory.
Then there's the second part, which in your case is very simple. It's simply one of two alternatives "domain1.com" or "domain2.com". The pipe seperates the alternatives, and the () brackets group characters together to make the alternatives for the pipe.
The regex that you had to start with also had ^ at the start and $ at the end. ^ denotes the beginning of a line, $ the end. As a regex checks for ANY part of a string, if you didn't include these you could end up with valid.email@domain1.com.mi
That changes the regex I gave you to:
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((d
Note the second change to the regex I originally gave, which is that instead of "domain1.com" I have changed it to "domain1\.com" (and the same for "domain2\.com"). The reason that that both worked in my tests, and won't work properly for you is that . represents "any single character" in a regex. So you escape it (in the regex) using a backslash, which makes it then mean "only a .".
Andy