日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

ASP.NET?MVC使用正則表達(dá)式驗(yàn)證手機(jī)號(hào)碼_實(shí)用技巧

作者:Darren?Ji ? 更新時(shí)間: 2022-11-04 編程語(yǔ)言

在ASP.NET MVC中,可以使用RegularExpression特性來(lái)驗(yàn)證手機(jī)號(hào)碼。

    public class Customer
    {
        [Required(ErrorMessage = "必填")]
        [Display(Name = "手機(jī)號(hào)")]
        [RegularExpression(@"^1[3458][0-9]{9}$", ErrorMessage = "手機(jī)號(hào)格式不正確")]
        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="提交"/>
}

原文鏈接:https://www.cnblogs.com/darrenji/p/4247994.html

欄目分類(lèi)
最近更新