文章詳情頁
ASP.NET MVC使用正則表達(dá)式驗(yàn)證手機(jī)號碼
瀏覽:70日期:2022-06-08 10:40:20
在ASP.NET MVC中,可以使用RegularExpression特性來驗(yàn)證手機(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中,必須引用異步驗(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ī)號碼的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
標(biāo)簽:
ASP.NET
相關(guān)文章:
1. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟2. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容3. ASP.NET MVC使用異步Action的方法4. ASP.NET MVC限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù)5. ASP.NET MVC使用Log4Net記錄異常日志并跳轉(zhuǎn)到靜態(tài)頁6. python通過re正則表達(dá)式切割中英文的操作7. ASP.NET MVC實(shí)現(xiàn)樹形導(dǎo)航菜單8. PHP擴(kuò)展之文本處理(二)——PCRE正則表達(dá)式語法1——分隔符9. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁10. 正則表達(dá)式在JavaScript應(yīng)用
排行榜
