基于其他字段的条件验证,c#

发布于 2024-10-29 10:14:49 字数 1219 浏览 4 评论 0原文

我需要创建几个场景:

1)如果下拉列表具有特定值,则将特定文本框设置为必填字段。

2)如果特定文本框有数据,则需要另一个文本框(如果填写了地址字段,则需要城市、州和邮政编码)

我有代码可以从一对看起来正确的 CustomValidators 中调用:

<asp:CustomValidator ID="cvtxt_pat_id" runat="server" 
     OnServerValidate="txt_pat_idValidate" ControlToValidate="txt_pat_id"
     ErrorMessage="Text must be 8 or more characters." Display="Dynamic"/>

protected void txt_pat_idValidate(object sender, ServerValidateEventArgs e)
{
    if (ddl_addl_pat_info.SelectedValue.ToString() == "2")
    {
        e.IsValid = (e.Value.Length > 1);
    }
    else
    {
        e.IsValid = true;
    }
}

<asp:CustomValidator ID="cvtxt_pat_id" runat="server" 
     OnServerValidate="addresspartsValidate" ControlToValidate="txt_city"
     ErrorMessage="Complete address must be entered." Display="Dynamic"/>

protected void addresspartsValidate(object sender, ServerValidateEventArgs e)
{
    if (txt_pat_address.Text.Length > 1)
    {
        e.IsValid = (e.Value.Length > 1);
    }
    else
    {
        e.IsValid = true;
    }
}

但据我了解,如果我正在测试的文本框是空的,该框永远不会验证,因此如果它们为空,则不会触发,这使得检查必填字段变得困难。那么……想法?

另外,关于我是否需要同时拥有测试的客户端和服务器版本,我收到了相互矛盾的故事。也许旧版本需要它,但现在不需要了?

I have a couple of scenarios I need to create:

1) if a dropdown has a particular value, make a particular textbox a required field.

2) if a particular textbox has data, make another textbox required (if an address field is filled in, require city, state and zip)

I have code to call from a pair of CustomValidators that looks right:

<asp:CustomValidator ID="cvtxt_pat_id" runat="server" 
     OnServerValidate="txt_pat_idValidate" ControlToValidate="txt_pat_id"
     ErrorMessage="Text must be 8 or more characters." Display="Dynamic"/>

protected void txt_pat_idValidate(object sender, ServerValidateEventArgs e)
{
    if (ddl_addl_pat_info.SelectedValue.ToString() == "2")
    {
        e.IsValid = (e.Value.Length > 1);
    }
    else
    {
        e.IsValid = true;
    }
}

<asp:CustomValidator ID="cvtxt_pat_id" runat="server" 
     OnServerValidate="addresspartsValidate" ControlToValidate="txt_city"
     ErrorMessage="Complete address must be entered." Display="Dynamic"/>

protected void addresspartsValidate(object sender, ServerValidateEventArgs e)
{
    if (txt_pat_address.Text.Length > 1)
    {
        e.IsValid = (e.Value.Length > 1);
    }
    else
    {
        e.IsValid = true;
    }
}

But as I understand it, if the textbox I'm testing is empty, the box never validates, so these don't fire if they're blank, which kind of makes it hard to check for a required field. So...thoughts?

Also, I'm getting conflicting stories as to whether or not I need to have BOTH a client and server version of my test. Perhaps it was required in an older version, and now isn't?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

吃颗糖壮壮胆 2024-11-05 10:14:49

你必须稍微向后思考一下。您的自定义验证器应该位于应显示错误的项目上(特定文本框)。文本框上的自定义验证器应检查下拉列表以查看下拉列表是否具有触发文本框所需条件所需的特定条件。如果发现为 true,那么您需要检查文本框是否有输入并相应地返回 args.IsValid。

protected void cvTimeOfDay_ServerValidate(object source, ServerValidateEventArgs args)
{
    if(ddlTimeOfDay.SelectedValue == "1" && txtbAddress.Text.Length == 0)
       args.IsValid = false;
    else
       args.IsValid = true;
}

var MyValidation = {
    DropdownValidation: function (sender, eventArgs) {
        var isValid;
        if (eventArgs && $('#ddlTimeOfDay').val() == '1') {
            isValid = false;
        }
        else
            isValid = true;
        eventArgs.IsValid = isValid; }
   }

<asp:DropDownList ID="ddlTimeOfDay" runat="server" ClientIDMode="Static">
<asp:ListItem Text="-Select-" Value="0"></asp:ListItem>
<asp:ListItem Text="PM" Value="1"></asp:ListItem>
<asp:ListItem Text="AM" Value="2"></asp:ListItem>
</asp:DropDownList>
 <br />
<asp:TextBox Text="" ID="txtbAddress" runat="server"  ClientIDMode="Static"></asp:TextBox>
<asp:CustomValidator ID="cvTimeOfDay" runat="server" 
    ErrorMessage="MustSelectValue" 
    ClientValidationFunction="MyValidation.DropdownValidation" 
    ControlToValidate="txtbAddress"  ValidationGroup="group1" 
    onservervalidate="cvTimeOfDay_ServerValidate" ValidateEmptyText="true"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button"  ValidationGroup="group1"/>

要使用自定义验证器验证空白文本框,您需要将 ValidateEmptyText 属性设置为“true”。

通常,如果您的网站不能确保打开 JavaScript 来使用该页面,则可以接受两者。有些浏览器可以关闭 JavaScript;如果 JavaScript 被关闭,它就会绕过你的验证。使用客户端验证很好,因为它不会每次都回发来验证输入,而是在客户端上正确执行。

You have to think about it a bit backward. Your custom validator should be on the item that should show the error (the Particular Textbox). The custom validator on the textbox should check the dropdown to see if the dropdown has the particular condition needed to trigger a required condition for the textbox. If it is found to be true then you want to check to see if the textbox has input and return args.IsValid accordingly.

protected void cvTimeOfDay_ServerValidate(object source, ServerValidateEventArgs args)
{
    if(ddlTimeOfDay.SelectedValue == "1" && txtbAddress.Text.Length == 0)
       args.IsValid = false;
    else
       args.IsValid = true;
}

var MyValidation = {
    DropdownValidation: function (sender, eventArgs) {
        var isValid;
        if (eventArgs && $('#ddlTimeOfDay').val() == '1') {
            isValid = false;
        }
        else
            isValid = true;
        eventArgs.IsValid = isValid; }
   }

<asp:DropDownList ID="ddlTimeOfDay" runat="server" ClientIDMode="Static">
<asp:ListItem Text="-Select-" Value="0"></asp:ListItem>
<asp:ListItem Text="PM" Value="1"></asp:ListItem>
<asp:ListItem Text="AM" Value="2"></asp:ListItem>
</asp:DropDownList>
 <br />
<asp:TextBox Text="" ID="txtbAddress" runat="server"  ClientIDMode="Static"></asp:TextBox>
<asp:CustomValidator ID="cvTimeOfDay" runat="server" 
    ErrorMessage="MustSelectValue" 
    ClientValidationFunction="MyValidation.DropdownValidation" 
    ControlToValidate="txtbAddress"  ValidationGroup="group1" 
    onservervalidate="cvTimeOfDay_ServerValidate" ValidateEmptyText="true"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button"  ValidationGroup="group1"/>

To Validate blank textbox with custom validator you need to set the ValidateEmptyText attribute to "true".

Generally using both is accepted if your site doesn't insure JavaScript is turned on to use the page. Some browsers can have JavaScript turned off; if JavaScript is turned off it bypasses your validation. Using a client side validation is good because it doesn't post back each time to validate input, it does it right on the client.

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