您可以放置一个文本框并使用正则表达式对其进行验证
在模型中:
public class Contact
{
[Required(ErrorMessage = "Invalid Phone")]
[RegularExpression(@"\d{3}-\d{4}-\d{3}", ErrorMessage = "Invalid Phone")]
public string Phone { get; set; }
}
如果你想使用 3 个文本框”:
public class Contact
{
[Required(ErrorMessage = "Invalid Phone")]
[RegularExpression(@"\d{3}", ErrorMessage = "Invalid Phone")]
public string Phone_Part1 { get; set; }
[Required(ErrorMessage = "Invalid Phone")]
[RegularExpression(@"\d{4}", ErrorMessage = "Invalid Phone")]
public string Phone_Part2 { get; set; }
[Required(ErrorMessage = "Invalid Phone")]
[RegularExpression(@"\d{3}", ErrorMessage = "Invalid Phone")]
public string Phone_Part3 { get; set; }
}