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-zA-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.
LVL 3
Paul KahlAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AGBrownCommented:
You would alter the regex to:
string strRegex = @"([a-zA-Z0-9_\-\.]+)@((domain1.com)|(domain2.com))";

Andy

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AGBrownCommented:
I should probably elaborate on that:

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.mistake instead of just valid.email@domain1.com. So to check the email address in full, you should probably add these to the start and the end of the expression I gave you respectively, that way it ensures that you don't have "domain1.com.mistake" instead of just "domain1.com".

That changes the regex I gave you to:
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((domain1\.com)|(domain2\.com))$";

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
Paul KahlAuthor Commented:
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_\-\.]+)@((domain1\.com)|(domain2\.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.txtEmailAddress.Text))
            lblErrorMessage.Text = this.txtEmailAddress.Text + " is not a valid email address.";
        else
            lblErrorMessage.Text = "Email Address is valid.";
    }

Thanks tremendously once again!
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

AGBrownCommented:
My pleasure. Its worth looking up Expresso if you are going to use more regular expressions in .NET.
Paul KahlAuthor Commented:
I thikn I have it installed, actually. I used it quite frequently in Studio 03.
AGBrownCommented:
Perfectly poised for world domination then ;-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.