我在使用 Luhn 算法验证信用卡时遇到问题。
我正在使用这个 c# 代码:
void ServerValidation(object source, ServerValidateEventArgs args)
{
// use a RequiredFieldValidator to check for an empty value
if (CreditCardNumber.Text == string.Empty) args.IsValid = true;
args.IsValid = IsCreditCardValid(this.CreditCardNumber.Text);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreditcardValidation.ServerValidate += ServerValidation;
}
public bool IsCreditCardValid(string cardNumber)
{
const string allowed = "0123456789";
int i;
StringBuilder cleanNumber = new StringBuilder();
for (i = 0; i < cardNumber.Length; i++)
{
if (allowed.IndexOf(cardNumber.Substring(i, 1)) >= 0)
cleanNumber.Append(cardNumber.Substring(i, 1));
}
if (cleanNumber.Length < 13 || cleanNumber.Length > 16)
return false;
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);
这在 aspx 中:
Creditcard number: <asp:TextBox ID="CreditCardNumber" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="CreditCardNumberReq" ControlToValidate="CreditCardNumber" ErrorMessage="Please enter a Number" runat="server" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="CreditcardValidation" ControlToValidate="CreditCardNumber" onservervalidate="ServerValidation" ErrorMessage="Please enter a valid Creditcard Number" runat="server" Display="Dynamic"></asp:CustomValidator>
我总是遇到错误,因为我没有从 .aspx 到 c# 或其他东西的值。这是我得到的具体错误:
说明:编译服务此请求所需的资源时出错。检查以下特定错误详细信息并适当修改您的源代码。
编译器错误消息:CS1061:“ASP.controls_midtermucc_ascx”不包含“服务器验证”的定义,并且没有找到接受“ASP.controls_midtermucc_ascx”类型的第一个参数的扩展方法“服务器验证”(你是缺少 using 指令或程序集引用?)
我需要做什么?