0
<asp:Label ID="lblDriving" runat="server" CssClass="lblDriving">Driving (y/n)</asp:Label>
<asp:TextBox ID="tbDriving" runat="server" CssClass="tbDriving"></asp:TextBox> 
<asp:RequiredFieldValidator ID="rfvDriving" runat="server" CssClass="errorAsterik" ErrorMessage="Please enter y or n in lowercase only" ControlToValidate="tbDriving">*</asp:RequiredFieldValidator>
<asp:RangeValidator ID="rvDriving" runat="server" CssClass="errorAsterik" ErrorMessage="Please enter n or y in lower case" ControlToValidate="tbDriving" MinimumValue="n" MaximumValue="y">*</asp:RangeValidator>

如何验证它以便用户可以使用 ASP.NET 验证器输入小写的 y/n 或大写的 Y/N?

4

2 回答 2

3

您可以使用正则表达式验证器:

<asp:RegularExpressionValidator ID="rvDriving" runat="server" 
   CssClass="errorAsterik" ErrorMessage="Please enter n or y" 
   ControlToValidate="tbDriving" 
   ValidationExpression="[yYnN]">*</asp:RegularExpressionValidator>
于 2013-02-28T10:53:55.177 回答
1

您将不得不使用 aCustomValidator因为CompareValidatorwithType=String比较区分大小写。

<asp:CustomValidator id="ValidDriving"
       ControlToValidate="tbDriving"
       ErrorMessage="Enter y/Y or n/N"
       OnServerValidate="ValidateDriving"
       ClientValidationFunction="ValidateDriving_JS" 
       runat="server"/>

服务器验证:

protected void ValidateDriving(object source, ServerValidateEventArgs args)
{
    args.IsValid = StringComparer.OrdinalIgnoreCase.Equals("y", args.Value) ||
                   StringComparer.OrdinalIgnoreCase.Equals("n", args.Value);
}

如果您还想提供客户端验证功能:

function ValidateDriving_JS(sender, args){
    var upperVal = args.Value.toUpperCase();
    args.IsValid = upperVal  == "Y" || upperVal == "N";
    return;      
}
于 2013-02-28T10:41:09.140 回答