asp.net CustomValidator 服务器端不会停止我的程序?

发布于 2024-12-29 11:04:01 字数 945 浏览 4 评论 0原文

我有一个绑定到数据库的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀梦 2025-01-05 11:04:01

您需要在注册按钮单击时强制验证:

this.Page.Validate();
if (this.Page.IsValid)
{
// your registration logic.
}

You need to force validation on register button click:

this.Page.Validate();
if (this.Page.IsValid)
{
// your registration logic.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文