0

我正在尝试根据下拉列表的选定值验证文本框。基本上我要做的是验证以确保信用卡号以 4 开头,如果选择了 Visa,或者如果选择了万事达卡,则以 5 开头。我还想根据给定的卡片类型验证字符串的长度。这是我到目前为止所拥有的。

            <p>
                Card Type:* <br />
                <asp:DropDownList ID="cardType" runat="server" AutoPostBack="True">
                    <asp:ListItem>Visa</asp:ListItem>
                    <asp:ListItem>Mastercard</asp:ListItem>
                    <asp:ListItem>Amex</asp:ListItem>
                </asp:DropDownList>
            </p>
            <p>
                Card Number:* <br />
                <asp:TextBox ID="cardNumber" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" 
                    ErrorMessage="Please enter Card Number" ControlToValidate="cardNumber"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" 
                    ControlToValidate="cardNumber" validationExpression="^\d+$" ErrorMessage="please only enter numbers"></asp:RegularExpressionValidator>
                <asp:CustomValidator ID="CustomValidator1" runat="server" 
                    ControlToValidate="cardNumber" ErrorMessage="Please enter correct number"></asp:CustomValidator>
            </p>
            <p>
                Security Code:* <br />
                <asp:TextBox ID="securityCode" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" 
                    ErrorMessage="Please enter Security Code" ControlToValidate="securityCode"></asp:RequiredFieldValidator>
                     <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" 
                    ControlToValidate="securityCode" validationExpression="^\d+$" ErrorMessage="please only enter numbers"></asp:RegularExpressionValidator>
                      <asp:RegularExpressionValidator ID="RegularExpressionValidator6" runat="server" 
                    ControlToValidate="securityCode" validationExpression="^\d\d\d?$" ErrorMessage="please only enter three numbers"></asp:RegularExpressionValidator>

            </p>
            <p>
                Expiration Date:* <br />
                <asp:TextBox ID="expDate" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" 
                    ErrorMessage="Please enter Expiration Date" ControlToValidate="expDate"></asp:RequiredFieldValidator>
                <asp:CompareValidator ID="dateValidator" runat="server" type="Date" ControlToValidate="expDate" Operator="DataTypeCheck"
                    ErrorMessage="please enter a valid date (mm/dd/yy)"></asp:CompareValidator>
            </p>

            &nbsp;<asp:Button ID="btnSubmit" runat="server" 
                    Text="Submit" />

4

2 回答 2

1

您可以实现如下所示:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    /*find length of the string using string.Length method*/
       int a = cardNumber.Text.Length;
   if(a<=your desired value)
   {
        /*give invalid length error*/
   }
   else
   { 
        if (cardType.SelectedValue == "Visa")
        {
            string str = cardNumber.Text;
            string firstChar = str.Substring(0, 1);
            if (firstChar != "4")
            {
                /*perform operation for invalid number*/

            }
            else
            {
                /*perform operation for valid number*/
             }
        }
        else if(cardType.SelectedValue == "MasterCard")
        {
             string str = cardNumber.Text;
            string firstChar = str.Substring(0, 1);
            if (firstChar != "5")
            {
                /*perform operation for invalid number*/

            }
            else
            {
                /*perform operation for valid number*/
             }                  
        }
   }
 }
于 2013-04-29T05:30:41.217 回答
0

我认为您需要做的就是在您的CardType.SelectedIndexChanged事件中设置适当的验证表达式。其余的应该自动跟随:

Private Sub cardType_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cardType.SelectedIndexChanged
    Select Case cardType.SelectedValue
        Case "Visa"
            RegularExpressionValidator4.validationExpression = "^4\d{15}$"  '-- 4 + any 15 digits
        Case "Mastercard"
            RegularExpressionValidator4.validationExpression = "^5\d{15}$"  '-- 5 + any 15 digits
        Case "Amex"
            RegularExpressionValidator4.validationExpression = "^\d+$"      '-- set this one also appropriately
    End Select
End Sub
于 2013-04-29T23:18:08.933 回答