I want validate the CNIC no(computed national identity card no). I want to take input from client like (12345-1234567-1) no character except dashes. I wrote below regular expression
[RegularExpression("^[0-9]{5}-[-|]-[0-9]{7}-[-|]-[0-9]{1}")]
public string NICNo { get; set; }
But when I submit the from it shows the whole regular expression in validation error Like below
You do not need [-|]
, use
^[0-9]{5}-[0-9]{7}-[0-9]$
See the regex demo. Note the $
at the end requiring the end of string.
Details
^
- start of string[0-9]{5}
- 5 digits-
- a hyphen[0-9]{7}
- 7 digits-
- a hyphen[0-9]
- a single digit (no need "saying" {1}
)$
- end of string anchor.In code:
[RegularExpression("^[0-9]{5}-[0-9]{7}-[0-9]$", ErrorMessage = "CNIC No must follow the XXXXX-XXXXXXX-X format!")]
public string NICNo { get; set; }