Asp.net-Mvc
Mvc驗證正則表達式只有數字?
我嘗試使用以下程式碼對 Mvc Web 應用程序中的聯繫人號碼驗證進行僅數字驗證。
[RegularExpression(@"/(^\(\d{10})?)$/", ErrorMessage = "Please enter proper contact details.")] [Required] [Display(Name = "Contact No")] public string ContactNo { get; set; }但是驗證表達式不起作用。
對於聯繫電話,我只想接受數字。它可以是 10 位數的手機號碼或固定電話號碼。
/ /是建構正則表達式文字對象的 javascript 方法。在 .NET 中,您不應該使用它。嘗試以下操作:
@"^\((\d{10}?)\)$"或者如果你想要 10 位數字:
@"^(\d{10})$"
如果除了數字之外沒有任何限制,這應該適合:
[RegularExpression(@"^\d+$", ErrorMessage = "Please enter proper contact details.")] [Required] [Display(Name = "Contact No")] public string ContactNo { get; set; }