【问题标题】:How to add a model validation for string [3 .. 4] characters如何为字符串 [3 .. 4] 字符添加模型验证
【发布时间】:2021-11-13 17:13:40
【问题描述】:

我正在使用这个 API https://developer-eu.elavon.com/docs/opayo/spec/api-reference-0#operation/createCi

在这个链接中,他们说

cardholderName
required
string <= 45 characters
The name of the card holder

cardNumber
required
string <= 16 characters
The number of the card

expiryDate
required
string 4 characters
The expiry date of the card in MMYY format

securityCode
required
string [ 3 .. 4 ] characters
The card security code, also known as CV2, CVV, or CVC. This is used in CV2 checks.

我为字符串长度验证添加了

[Required]
[StringLength(45)]
public string CardholderName { get; set; }

在此 API 中,他们验证 securityCode 是 POST 请求所需的字符串 [3 .. 4 ] 个字符。

如何添加验证 securityCode required string [ 3 .. 4 ] characters?

【问题讨论】:

  • “为此添加验证”到底是什么意思?
  • 我已经添加了更多。请检查
  • StringLengthAttribute 可以指定最小值和最大值。
  • [RegularExpression("^(0[1-9])|(1[0-2])2[0-9]$")] - 我们接受一年中的任何月份 [20]20..29

标签: c# asp.net-core validation


【解决方案1】:

我建议使用正则表达式,例如

 [RegularExpression("^(0[1-9])|(1[0-2])2[1-9]$")] 
 public string ExpireDate {get; set;}

这里我们接受01..12 范围内的任何月份和21..29 范围内的任何年份。安全码也一样:

 [RegularExpression("^[0-9]{3,4}$")]
 public string SecurityCode {get; set;}

这里我们接受34 数字

【讨论】:

  • 谢谢。是否有像 [StringLength(x)] 这样的字符串 [x..y] 字符的特定验证器?因为我需要更多这样的验证。”browserAcceptHeader required string [ 1 .. 2048 ] characters Exact content of the HTTP accept headers as sent to the 3DS Requestor from the Cardholder's browser.
  • @hanushic:你可以使用^.{x,y}$模式:.代表正则表达式中的任何符号;例如[RegularExpression("^.{1,2048}$")]
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
相关资源
最近更新 更多