How to add a model validation for string [3 .. 4] characters

Viewed 150

I am working this API https://developer-eu.elavon.com/docs/opayo/spec/api-reference-0#operation/createCi.

In this link, they said

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.

I added for string length validation as

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

In this API, they validate securityCode is a requiredstring [ 3 .. 4 ] characters for a POST request.

How to add a validation for securityCode required string [ 3 .. 4 ] characters?

1 Answers

I suggest using regular expressions, e.g.

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

Here we accept any month in 01..12 range for any year in 21..29 range. Same for security code:

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

Here we accept 3 or 4 digits

Related