Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression error message

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

like image 415
John Avatar asked Jun 25 '13 11:06

John


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What does $1 do in regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.


1 Answers

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; }
like image 115
Darin Dimitrov Avatar answered Sep 21 '22 13:09

Darin Dimitrov