文章詳情頁
ASP.NET MVC使用正則表達(dá)式驗(yàn)證手機(jī)號(hào)碼
瀏覽:262日期:2022-06-08 10:40:20
在ASP.NET MVC中,可以使用RegularExpression特性來驗(yàn)證手機(jī)號(hào)碼。
public class Customer { [Required(ErrorMessage = "必填")] [Display(Name = "手機(jī)號(hào)")] [RegularExpression(@"^1[3458][0-9]{9}$", ErrorMessage = "手機(jī)號(hào)格式不正確")] 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中,必須引用異步驗(yàn)證的相關(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使用正則表達(dá)式驗(yàn)證手機(jī)號(hào)碼的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
ASP.NET
相關(guān)文章:
1. ASP.NET MVC使用typeahead.js實(shí)現(xiàn)輸入智能提示功能2. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息3. ASP.NET MVC解決上傳圖片臟數(shù)據(jù)的方法4. ASP.Net MVC利用NPOI導(dǎo)入導(dǎo)出Excel的示例代碼5. PHP一些常用的正則表達(dá)式6. python正則表達(dá)式re.match()匹配多個(gè)字符方法的實(shí)現(xiàn)7. python re模塊和正則表達(dá)式8. Python正則表達(dá)式高級(jí)使用方法匯總9. Python中正則表達(dá)式對(duì)單個(gè)字符,多個(gè)字符和匹配邊界等使用10. idea 正則表達(dá)式搜索替換應(yīng)用詳解
排行榜
