I am trying to use a regular expression to validate a phone number and return an error when an invalid number or phone number is submitted.
MVC Code:
<ol class="row">
<li class="cell" style="width: 20%;">Phone Number:</li>
<li class="cell last" style="width: 60%;">
@Html.TextBoxFor(model => model.PhoneNumber, new { @class = "textbox" })
@Html.ValidationMessageFor(model => model.PhoneNumber)
</li>
</ol>
C# Code:
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone Number")]
[Required(ErrorMessage = "Phone Number Required!")]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$",
ErrorMessage = "Entered phone format is not valid.")]
public string PhoneNumber { get; set; }
However, the input box will not show a message to the user indicating that the phone number which was submitted is not valid.
@ValidationFor()
on the page. – Putto