asp.net CustomValidator 服务器端不会停止我的程序?
我有一个绑定到数据库的 DropDownList。我还手动向其添加一个项目“(其他)”,
当用户选择“(其他)”时,JQuery 会触发 .Show()
隐藏的
我正在尝试验证这个文本框。当然,因为我只是使用客户端隐藏它,所以我不能使用RequiredFieldValidator+RegularExpressionValidator,所以我尝试了一个我不太熟悉的CustomValidator:
protected void validatorOther(object sender, ServerValidateEventArgs e)
{
if (dropdownVisitorType.SelectedItem.ToString() == "(other)")
{
e.IsValid = (textboxOtherVisitorType.Text != "");
}
}
protected void buttonRegister_Click(object sender, EventArgs e)
{
//a whole bunch of code here...
}
然后从我的aspx
<asp:CustomValidator runat="server" id="validatorOtherVisitorType" ValidateEmptyText="true" onservervalidate="validatorOther" errormessage="*" />
当我尝试调试时,似乎e.IsValid
将成功返回 false
。然而,我的网页似乎只是忽略它并继续进行,使得验证器毫无用处。我做错了什么?
I have a DropDownList bound to a DB. I also manually add to it an item "(other)"
When a user selects "(other)", JQuery fires and .Show()
a hidden <asp:TextBox>
where the user must input something.
I am attempting to validate this TextBox. Of course since I'm just hiding it using client-side, I can't use RequiredFieldValidator+RegularExpressionValidator so I tried a CustomValidator which I'm not very familiar with:
protected void validatorOther(object sender, ServerValidateEventArgs e)
{
if (dropdownVisitorType.SelectedItem.ToString() == "(other)")
{
e.IsValid = (textboxOtherVisitorType.Text != "");
}
}
protected void buttonRegister_Click(object sender, EventArgs e)
{
//a whole bunch of code here...
}
And then from my aspx
<asp:CustomValidator runat="server" id="validatorOtherVisitorType" ValidateEmptyText="true" onservervalidate="validatorOther" errormessage="*" />
When I try to debug, it seems that e.IsValid
will successfully return false
. However, my webpage seems to just ignore it and proceed anyway, making the validator useless. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在注册按钮单击时强制验证:
You need to force validation on register button click: