共计 1035 个字符,预计需要花费 3 分钟才能阅读完成。
导读 | 这篇文章介绍了 ASP.NET MVC 使用正则表达式验证手机号码的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 |
在 ASP.NET MVC 中,可以使用 RegularExpression 特性来验证手机号码。
public class Customer | |
{[Required(ErrorMessage = "必填")] | |
[ | ]|
[ | ]|
public string PhoneNumber {get; set;} | |
} |
在 HomeController 中:
public class HomeController : Controller | |
{public ActionResult Index() | |
{return View(new Customer()); | |
} | |
[ | ]|
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 中:
MvcApplication1.Models.Customer | |
@{ | |
ViewBag.Title = "Index"; | |
Layout = "~/Views/Shared/_Layout.cshtml"; | |
} | |
Index | |
Html.BeginForm("Index", "Home", FormMethod.Post)) | (|
{LabelFor(m => m.PhoneNumber) | .|
TextBoxFor(m => m.PhoneNumber) | .|
ValidationMessageFor(m => m.PhoneNumber) | .|
} |
到此这篇关于 ASP.NET MVC 使用正则表达式验证手机号码的文章就介绍到这了。
正文完
星哥玩云-微信公众号
