2013-04-29 107 views
0

我想驗證基於選定的下拉列表的值的文本框。基本上我想要做的是驗證以確保信用卡號碼以4開頭,如果選擇簽證或選擇5如果mastercard被選中。我也想根據給定的卡類型驗證字符串的長度。這是我到目前爲止。基於下拉列表選擇驗證文本框

  <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" /> 

回答

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*/ 
      }     
     } 
    } 
} 
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 
相關問題