Search code examples
asp.net-mvc-3data-annotations

Negate credit card number RegEx match using Data annotations in asp.net mvc


[RegularExpression(@"^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$",ErrorMessage ="Name cannot contain credit card number")]
[Required(ErrorMessage = "Card holder name is required")]
public string CardHolderName { get; set; }

It detects the credit card as expected, but I need it to do the opposite. I need it to give me a warning if it detects the credit card saying that "name cannot contain a credit card numbers".


Solution

  • You can try with this regex. If you want to do the opposite of above mentioned regex.

    ^(?!(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$)