.NET 4.5 包含一个名为 CreditCardAttribute
的新验证属性该属性指定数据字段值为信用卡号。当我反编译包含此类的程序集时,我可以看到以下用于信用卡号验证的代码:
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
string text = value as string;
if (text == null)
{
return false;
}
text = text.Replace("-", "");
text = text.Replace(" ", "");
int num = 0;
bool flag = false;
foreach (char current in text.Reverse<char>())
{
if (current < '0' || current > '9')
{
return false;
}
int i = (int)((current - '0') * (flag ? '\u0002' : '\u0001'));
flag = !flag;
while (i > 0)
{
num += i % 10;
i /= 10;
}
}
return num % 10 == 0;
}
有人知道这里应用了哪种算法来验证数字格式吗?卢恩的算法?另外,这是ISO标准吗?最后,您认为这是正确且 100% 正确的实现吗?
MSDN 没有提供太多这方面的信息。事实上,他们有如下错误信息:
Remarks
The value is validated using a regular expression. The class does not validate that the credit card number is valid for purchases, only that it is well formed.
最佳答案
关于c# - CreditCardAttribute 使用哪种算法进行信用卡号格式验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12580450/