文章详情页
ASP.NET MVC使用正则表达式验证手机号码
浏览:75日期: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. ASP.NET MVC使用Quartz.NET执行定时任务6. ASP.NET MVC前台动态添加文本框并在后台使用FormCollection接收值7. JS正则表达式常见函数与用法小结8. PHP扩展之文本处理(二)——PCRE正则表达式语法13——条件子组9. ASP.NET MVC使用jQuery ui的progressbar实现进度条10. python中使用正则表达式将所有符合条件的字段全部提取出来
排行榜
