在Page.Load中,我动态地添加了一个标签、应该被验证的文本框和自定义验证器。
Label myLabel3 = new Label();
myLabel3.ID = "lblEGN" + i.ToString();
myLabel3.Text = "EГН";
TextBox myTextBox3 = new TextBox();
myTextBox3.ID = "txtEGN" + i.ToString();
pnlPersonalCard.Controls.Add(myLabel3);
pnlPersonalCard.Controls.Add(new LiteralControl("<br />"));
CustomValidator cvEGN = new CustomValidator();
cvEGN.ID = "cvtxtEGN" + i.ToString();
cvEGN.ControlToValidate = "txtEGN" + i.ToString();
// cvEGN.ClientValidationFunction = "checkEgn";
cvEGN.ServerValidate += serverCheckEgn;
cvEGN.ErrorMessage = "Невалидно егн";
pnlPersonalCard.Controls.Add(cvEGN);
pnlPersonalCard.Controls.Add(myTextBox3);
pnlPersonalCard.Controls.Add(new LiteralControl("<br />"));当然,我提供了应该执行的自定义验证函数。
protected void serverCheckEgn(object sender, ServerValidateEventArgs args) {
string egn = args.Value;
if (egn.Length != 10)
args.IsValid = false;
int year = Int32.Parse(egn.Substring(0, 2));
int month = Int32.Parse(egn.Substring(2, 4));
int day = Int32.Parse(egn.Substring(4, 6));
if (month >= 40) {
year += 2000;
month -= 40;
} else if (month >= 20) {
year += 1800;
month -= 20;
} else {
year += 1900;
}
string date = year + "/" + month + "/" + day;
if (!CheckDate(date))
args.IsValid=false;
int checkSum = 0;
int[] weights = new int[9] {2,4,8,5,10,9,7,3,6};
for (var ii = 0; ii < weights.Length; ++ii) {
checkSum += weights[ii] * Int32.Parse(egn.Substring(ii,1));
}
checkSum %= 11;
checkSum %= 10;
if (checkSum != Int32.Parse(egn.Substring(9,1)))
args.IsValid=false;
args.IsValid = true;
}但是,当我按下我的按钮时,所有其他的验证器(我有另外两个需求字段验证器和另外两个正则域验证器)都在工作,只有这个自定义验证器才能工作,不是我提供的函数没有执行吗!
发布于 2013-09-19 09:46:08
在插入记录到数据库期间,请检查IsValid:
if (page.IsValid)
{
//insert record
}
else
{
Response.Write("Input string is incorrect!");
}发布于 2013-09-19 08:50:55
当您试图验证时,文本框的内容是否为空?要验证何时为空,需要将ValidateEmptyText属性设置为true
发布于 2013-09-19 09:35:16
伙计们,当我在文本框- 5555中写时,你真不敢相信是什么导致了这个问题。
当程序到达这一行时
int day = Int32.Parse(egn.Substring(4, 6));当然,我得到的例外是,我想要减的字符超出了我的字符串的范围。
问题是另一个问题。即使在我的button_click函数中,我也得到了这一行
if (page.IsValid) 在插入数据库之前检查是否一切正常;我不知道为什么,但是通过args.IsValid=false将记录插入到数据库事件中
https://stackoverflow.com/questions/18889830
复制相似问题