文章詳情頁
ASP.NET MVC使用正則表達式驗證手機號碼
瀏覽:74日期:2022-06-08 10:40:20
在ASP.NET MVC中,可以使用RegularExpression特性來驗證手機號碼。
public class Customer { [Required(ErrorMessage = "必填")] [Display(Name = "手機號")] [RegularExpression(@"^1[3458][0-9]{9}$", ErrorMessage = "手機號格式不正確")] public string PhoneNumber { get; set; } }
在HomeController中:
public class HomeController : Controller { public ActionResult Index() { return View(new Customer()); } [HttpPost] public ActionResult Index(Customer customer) { if (ModelState.IsValid) { return Content("ok"); } else { return View(customer); } } }
在Views/Shared/_Layout.cshtml中,必須引用異步驗證的相關js。
@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval")
在Home/Index.cshtml中:
@model MvcApplication1.Models.Customer @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> @using (Html.BeginForm("Index", "Home", FormMethod.Post)) { @Html.LabelFor(m => m.PhoneNumber) @Html.TextBoxFor(m => m.PhoneNumber) @Html.ValidationMessageFor(m => m.PhoneNumber) <br/> <input type="submit" value="提交"/> }
到此這篇關于ASP.NET MVC使用正則表達式驗證手機號碼的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持。
標簽:
ASP.NET
相關文章:
1. python通過re正則表達式切割中英文的操作2. js用正則表達式篩選年月日的實例方法3. Python中正則表達式對單個字符,多個字符和匹配邊界等使用4. ASP.NET MVC獲取多級類別組合下的產品5. 一篇文章帶你了解python正則表達式的正確用法6. ASP.NET MVC實現區域或城市選擇7. ASP.NET MVC使用Quartz.NET執行定時任務8. ASP.NET MVC前臺動態添加文本框并在后臺使用FormCollection接收值9. PHP擴展之文本處理(二)——PCRE正則表達式語法13——條件子組10. ASP.NET MVC使用jQuery ui的progressbar實現進度條
排行榜
