我正在尝试根据下拉列表的选定值验证文本框。基本上我要做的就是验证以确保信用卡号码以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>
<asp:Button ID="btnSubmit" runat="server"
Text="Submit" />
答案 0 :(得分: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*/
}
}
}
}
答案 1 :(得分: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