2
@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?

2
  • possible duplicate of how to do an input with a mask Commented Jul 31, 2013 at 10:26
  • use masking for that. Commented Jul 31, 2013 at 10:28

2 Answers 2

2

There is no way masked Html.Textbox, you can use jquery for this (link) Or you can use DataAnnotation DisplayFormat for this

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.