文章詳情頁
ASP.NET MVC使用正則表達式驗證手機號碼
瀏覽:85日期: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中,必須引用異步驗證的相關(guān)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="提交"/> }
到此這篇關(guān)于ASP.NET MVC使用正則表達式驗證手機號碼的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持。
標簽:
ASP.NET
相關(guān)文章:
1. ASP.NET MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值2. python正則表達式的懶惰匹配和貪婪匹配說明3. ASP.NET MVC使用異步Action的方法4. PHP編程技巧:看實例學正則表達式5. java正則表達式匹配所有數(shù)字的案例6. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容7. javascript正則表達式檢驗8. 用正則表達式和javascript對表單進行全面驗證9. ASP.NET MVC視圖頁使用jQuery傳遞異步數(shù)據(jù)的幾種方式詳解10. java正則表達式之Pattern與Matcher類詳解
排行榜
