文章詳情頁
ASP.NET MVC使用正則表達(dá)式驗證手機(jī)號碼
瀏覽:93日期:2022-06-08 10:40:20
在ASP.NET MVC中,可以使用RegularExpression特性來驗證手機(jī)號碼。
public class Customer { [Required(ErrorMessage = "必填")] [Display(Name = "手機(jī)號")] [RegularExpression(@"^1[3458][0-9]{9}$", ErrorMessage = "手機(jī)號格式不正確")] 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使用正則表達(dá)式驗證手機(jī)號碼的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
ASP.NET
相關(guān)文章:
1. ASP.NET MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值2. ASP.NET MVC獲取多級類別組合下的產(chǎn)品3. ASP.NET MVC使用jQuery的Load方法加載靜態(tài)頁面及注意事項4. ASP.NET MVC增加一條記錄同時添加N條集合屬性所對應(yīng)的個體5. ASP.NET MVC使用Log4Net記錄異常日志并跳轉(zhuǎn)到靜態(tài)頁6. ASP.NET MVC遍歷驗證ModelState的錯誤信息7. ASP.NET MVC實現(xiàn)區(qū)域或城市選擇8. ASP.NET MVC實現(xiàn)橫向展示購物車9. 使用EF Code First搭建簡易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫遷移10. ASP.NET MVC使用typeahead.js實現(xiàn)輸入智能提示功能
排行榜
