2

Possible Duplicate:
Email Address Validation for ASP.NET

 <input id="txtemail" type="text" />

 private void btnDelete_Click(object sender, EventArgs e)
 {
     //txtemail check is email or not
 }

how can check that content txtemail is email.

Community
  • 1
  • 1
GodIsLive
  • 33
  • 2
  • 6

4 Answers4

6

Why don't you just use build-in System.Net.Mail.MailAddress class for email validation?

private void btnDelete_Click(object sender, EventArgs e)
{
    bool isValidEmail = false;
    try
    {
        var email = new MailAddress(txtEmail.Text);
        isValidEmail = true;
    {
    catch
    {
    }
}

Note: as mentioned in the comments, it might give unexpected results. A mail address here is composed of a User name, Host name and optionally, a DisplayName. So, in this case you could additionaly verify the DisplayName, and if it is not empty also return false.

Oleks
  • 31,955
  • 11
  • 77
  • 132
  • 1
    This might give unexpected results. For instance, given the string john doe@mail.com, this is considered to be valid email. Where doe@mail.com is the email and john the display name. – coffeeyesplease Dec 06 '13 at 16:51
  • @coffeeyesplease: thank you, it is fair enough :) I updated my answer. – Oleks Dec 06 '13 at 17:36
1

in c#

take regular expresion validator and set its poperty "VALIDATION EXPRESION" via go in property window.

and put expesion is follow.

^([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})(]?)$

try this..........

SHRUTI PATEL
  • 125
  • 4
0

Your best bet is to use a email validating regex to verify that the txtemail.text is a valid email. A word of caution though, if you are going to do this make sure to implement a robust solution as nothing chokes users more than when they enter a valid email and have the page reject it.

Cody
  • 3,734
  • 2
  • 24
  • 29
-1

Read these posts:

Others:

Community
  • 1
  • 1
Naveed
  • 41,517
  • 32
  • 98
  • 131
  • This incorrectly classifies emails with plus signs in them as invalid. There's also probably some other flaws, too. – icktoofay Mar 16 '11 at 08:27