文本框电话号码格式

时间:2021-12-13 18:24:08
@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)

我有文本框。我只想输入电话号码。格式为+1(_)-___共10个数(3+3+4)

How can I do it in Html.Textbox in asp.net mvc?

我怎么用Html来做呢?文本框在asp.net mvc吗?

2 个解决方案

#1


2  

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

不可能屏蔽Html。Textbox,您可以使用jquery进行链接,也可以使用DataAnnotation DisplayFormat进行链接

#2


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)

#1


2  

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

不可能屏蔽Html。Textbox,您可以使用jquery进行链接,也可以使用DataAnnotation DisplayFormat进行链接

#2


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)