Using the RegularExpression(@"^\d{1,15}$")]
, I want the user to enter digits up to 15 in length, which returns the error message 'Please enter up to 15 digits for a contact number' if this is not correct
[Required(ErrorMessage = ("Please enter up to 15 digits for a contact number")), Display(Name = "Contact Number"), RegularExpression(@"^\d{1,15}$")]
public string ContactNumber { get; set; }
If the user fails to do this I am left with the error message:
The field Contact Number must match the regular expression '^\d{1,15}$'.
instead of 'Please enter up to 15 digits for a contact number'
...does anyone know why?
thanks
$ means "Match the end of the string" (the position after the last character in the string).
For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.
You have assigned the ErrorMessage
to the RequiredAttribute
(which you absolutely don't need in this case because of the regular expression). So:
[Display(Name = "Contact Number")]
[RegularExpression(@"^\d{1,15}$", ErrorMessage = "Please enter up to 15 digits for a contact number")]
public string ContactNumber { get; set; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With