3

我有几个需要创建的场景:

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;
    }
}

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

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

4

1 回答 1

3

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.

于 2011-04-01T22:55:12.243 回答