您好,我的项目有问题。当我提交信用卡号码时,程序通常应该跳转到我的 servervalidation,执行 if 指令中的命令并跳转到两个 bool 方法,但程序只是通过 if 指令并跳转到 submitbutton 方法以显示结果。我做错了什么?
这里是服务器的代码:
protected void SubmitMidtermBtn_Click(object sender, EventArgs e)
{
this.OnMidtermInfoCollected();
}
public void ServerValidation(object source, ServerValidateEventArgs args)
{
if (CardType(args.Value, CreditCardType.Text) && IsCreditCardValid(args.Value))
{
args.IsValid=true;
}
else
{
args.IsValid=false;
}
}
public bool CardType(string cardNumber, string cardType)
{
if (cardType.Equals("VISA"))
{
if (cardNumber.Length == 14 || cardNumber.Length == 16 &&
(cardNumber.StartsWith("4")))
{
return true;
}
}
if (cardType.Equals("Mastercard"))
{
if ( cardNumber.Length == 16 &&
cardNumber.StartsWith("51") ||
cardNumber.StartsWith("52") ||
cardNumber.StartsWith("53") ||
cardNumber.StartsWith("54") ||
cardNumber.StartsWith("55"))
{
return true;
}
}
if (cardType.Equals("AmericanExpress"))
{
if (cardNumber.Length == 15 &&
cardNumber.StartsWith("34") ||
cardNumber.StartsWith("37"))
{
return true;
}
}
if (cardType.Equals("Discover"))
{
if (cardNumber.Length == 16 &&
cardNumber.StartsWith("601"))
{
return true;
}
}
return false;
}
public bool IsCreditCardValid(string cardNumber)
{
int i;
StringBuilder cleanNumber = new StringBuilder();
for (i = cleanNumber.Length + 1; i <= 16; i++)
cleanNumber.Insert(0, "0");
int multiplier, digit, sum, total = 0;
string number = cleanNumber.ToString();
for (i = 1; i <= 16; i++)
{
multiplier = 1 + (i % 2);
digit = int.Parse(number.Substring(i - 1, 1));
sum = digit * multiplier;
if (sum > 9)
sum -= 9;
total += sum;
}
return (total % 10 == 0);
}
这个东西没有抓住应该在这个地方的最后一个结束标签
此时应该激活服务器验证:
<asp:CustomValidator ID="CreditcardValidation"
ControlToValidate="CreditCardNumber"
OnServerValidate="ServerValidation"
ErrorMessage=""
runat="server" Display="Dynamic">
<div class="alert alert-danger">Please enter a valid creditcard number</div>
</asp:CustomValidator>
有人可以帮我吗?