I have a @Html.TextBoxFor control with @Html.ValidationMessageFor, which is not a Required Field, but there is a limit in Maximum Length and also only numbers should be permitted. I have given the following code in the ViewModel :
[StringLength(10, ErrorMessage = "The field Phone must have a maximum length of 10")]
public string Phone { get; set; }
On the button click, I am unable to retrieve the above validation message while I had entered more than 10 characters.
And also How will I validate whether numeric ?
The controller action that you are posting to must take the view model containing the
Phoneproperty as parameter:or if this is not the case you should call the
UpdateModelorTryUpdateModelmethods inside to invoke the default model binder which is responsible for calling the validation logic and inserting any potential error messages into the ModelState.You could use a Regex: