【发布时间】:2013-05-09 02:35:13
【问题描述】:
大家:
我正在尝试验证表单上文本框中的数据。这是我的验证器的代码,但是当我明显违反代码中设置的约束时它不起作用(从 txtbox 中跳出,输入少于 4 个字符等。关于为什么这不起作用的任何想法?程序运行,所以不可能是语法,所以我想一定是逻辑错误,但我就是没看到。
// Validation for Applicant Name text box
private void txtAppInfoName_Validating(object sender, CancelEventArgs e)
{
// Define what consitiutes a match
Match m = Regex.Match(txtAppInfoName.Text, @"\b[A-Za-z]\b");
if (m.Success == false)
{
errorProvider1.SetError(txtAppInfoName, "Please use only letters to type your name.");
txtAppInfoName.Select();
}
else if (txtAppInfoName.Text.Length < 4)
{
errorProvider1.SetError(txtAppInfoName, "Please type first and last names.");
txtAppInfoName.Select();
}
else if (txtAppInfoName.Text == "")
{
errorProvider1.SetError(txtAppInfoName, "Must enter a name.");
txtAppInfoName.Select();
}
else errorProvider1.SetError(txtAppInfoName, ""); // Remove the error provider
}
【问题讨论】:
-
您是否单步执行了您的代码?
标签: c# error-handling validation