How to apply validation for email id text box in window phone 7?
I want to apply validation to text box. How t开发者_StackOverflow中文版o apply validation for email id text box in window phone 7?
public static bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn,
@"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
}
using System;
using System.Text.RegularExpressions;
Change your email TextBox's InputScope property to EmailNameAddress and use this
Match match = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$").Match(txtEmail.Text);
if (match.Success)
{
MessageBox.Show("Valid Email");
} else MessageBox.Show("Invalid Email");
You can change the InputScope property of the textbox into EmailNameAddress
//Say the text box's name as txtEmail
using System;
using System.Text.RegularExpressions;
private void txtEmail_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
string errorMsg;
if(!ValidEmailAddress(txtEmail.Text, out errorMsg))
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
txtEmail.Select(0, txtEmail.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(txtEmail, errorMsg);
}
}
private void txtEmail_Validated(object sender, System.EventArgs e)
{
// If all conditions have been met, clear the ErrorProvider of errors.
errorProvider1.SetError(txtEmail, "");
}
public bool ValidEmailAddress(string emailAddress, out string errorMessage)
{
// Confirm that the e-mail address string is not empty.
if(emailAddress.Length == 0)
{
errorMessage = "e-mail address is required.";
return false;
}
// Confirm that the e-mail address in correct regex format
if (Regex.IsMatch(emailAddress,
@"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$")==false)
// Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
if(emailAddress.IndexOf("@") > -1)
{
if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") )
{
errorMessage = "";
return true;
}
}
errorMessage = "e-mail address must be valid e-mail address format.\n" +
"For example 'someone@example.com' ";
return false;
}
if (string.IsNullOrEmpty(email)) return false; var regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)@\w+([-.]\w+).\w+([-.]\w+)*"); return regex.IsMatch(email) && !email.EndsWith(".");
Write a helper method like below :
private bool IsVaildEmail(string email)
{
return Regex.IsMatch(email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z");
}
Call this method and pass the email id which returns true if matched and false if unmatched. Simple.
精彩评论