validation email [duplicate]
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.
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
.
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..........
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.
Read these posts:
- Email Address Validation for ASP.NET
- Is RegEx used by System.Net.Mail.MailAddress
Others:
- Effective Email Address Validation
- Email Address Validation Using Regular Expression
精彩评论