@Html.TextBox("MyTextBox", "Value", new {style = "width:250px"})
I have textbox. I want to enter only phone numbers. The format is +1 (_) -___ Total 10 numbers (3+3+4)
How can I do it in Html.Textbox
in asp.net mvc?
@Html.TextBox("MyTextBox", "Value", new {style = "width:250px"})
I have textbox. I want to enter only phone numbers. The format is +1 (_) -___ Total 10 numbers (3+3+4)
How can I do it in Html.Textbox
in asp.net mvc?
There is no way masked Html.Textbox, you can use jquery for this (link) Or you can use DataAnnotation DisplayFormat for this
You can use a RegularExpression attribute in your Model like this:
public class MyModel
{
// The regex maches this pattern: "+1 (xxx) xxx-xxxx"
[RegularExpression(@"^\+1 \(\d{3}\) \d{3}-\d{4}$", ErrorMessage = "Invalid Phone Number.")]
public string PhoneNumber { get; set; }
}
Then, in your View, you'll have:
@Html.TextBoxFor(model => model.PhoneNumber)
@Html.ValidationMessageFor(model => model.PhoneNumber)