这篇文章介绍了ASP.NET MVC使用正则表达式验证手机号码的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
在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";
}
Index
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    @Html.LabelFor(m => m.PhoneNumber)
    @Html.TextBoxFor(m => m.PhoneNumber)
    @Html.ValidationMessageFor(m => m.PhoneNumber)
    
    type="submit" value="提交"/>
}
到此这篇关于ASP.NET MVC使用正则表达式验证手机号码的文章就介绍到这了。
以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

		
			    	微信扫一扫打赏
			    
			    	支付宝扫一扫打赏	
			    