我想在文本框中应用验证。 如何在窗口电话7中应用电子邮件ID文本框的验证?
答案 0 :(得分:6)
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;
答案 1 :(得分:3)
将您的电子邮件TextBox的InputScope属性更改为EmailNameAddress 并使用此
Match match = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$").Match(txtEmail.Text);
if (match.Success)
{
MessageBox.Show("Valid Email");
} else MessageBox.Show("Invalid Email");
答案 2 :(得分:2)
答案 3 :(得分:0)
//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;
}
答案 4 :(得分:0)
if(string.IsNullOrEmpty(email)) 返回false; var regex = new System.Text.RegularExpressions.Regex(@“\ w +([ - +。'] \ w +) @ \ w +([ - 。] \ w +)。\ w +([ - ] \ W +)*“); return regex.IsMatch(email)&amp;&amp; !email.EndsWith( “”);
答案 5 :(得分:0)
编写如下的辅助方法:
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");
}
调用此方法并传递电子邮件ID,如果匹配则返回true,如果不匹配则返回false。简单。