• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

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.
0
Paul Kahl
Asked:
Paul Kahl
  • 4
  • 2
1 Solution
 
AGBrownCommented:
You would alter the regex to:
string strRegex = @"([a-zA-Z0-9_\-\.]+)@((domain1.com)|(domain2.com))";

Andy
0
 
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
0
 
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!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AGBrownCommented:
My pleasure. Its worth looking up Expresso if you are going to use more regular expressions in .NET.
0
 
Paul KahlAuthor Commented:
I thikn I have it installed, actually. I used it quite frequently in Studio 03.
0
 
AGBrownCommented:
Perfectly poised for world domination then ;-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now