gpt4 book ai didi

c# - Asp.Net MVC 4 信用卡验证器不适用于开关

转载 作者:太空狗 更新时间:2023-10-29 21:39:29 25 4
gpt4 key购买 nike

有谁知道 ASP.Net MVC 4 上的 CreditCardAttribute 类是否会阻止交换卡的验证?

在我的 View 模型中,我将其设置为:

[CreditCard]
[Required]
[Display(Name = "Card Number")]
public string CardNumber { get; set; }

我已经用 Visa 和 Mastercard 测试过,但是当输入 Switch 卡时,它不允许它通过。

最佳答案

我有类似的问题,所以我放弃了验证并允许我的支付提供商为我验证卡。它的计算成本更高一些,但内置的 CreditCardAttribute 似乎很糟糕。我的解决方案:

[Display(Name = "Credit Card Number")]
[Required(ErrorMessage = "required")]
[Range(100000000000, 9999999999999999999, ErrorMessage = "must be between 12 and 19 digits")]
public long CardNumber { get; set; }

这是 .NET 4.0 CreditCardAttribute 类代码,您可以调整它来创建您自己的信用卡验证属性。你会看到他们做了一个“mod 10”的变体 AKA Luhn Algorithm 但似乎并未严格遵守,这可能是 Switch 无法验证的原因。代码:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class CreditCardAttribute : DataTypeAttribute
{
public CreditCardAttribute() : base(DataType.CreditCard)
{
base.ErrorMessage = DataAnnotationsResources.CreditCardAttribute_Invalid;
}
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;
}
}

关于c# - Asp.Net MVC 4 信用卡验证器不适用于开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21242739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com