ASP.NET MVC使用正则表达式验证手机号码

2023-12-05 0 255

在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中17种常用正则表达式
  • 正则表达式速查表(ASP.NET)
  • asp.net中利用正则表达式判断一个字符串是否为数字的代码
  • asp.net 正则表达式的应用
  • asp.net 正则表达式[经常用的到]
  • asp.net常用正则表达式
  • asp.net验证一个字符串是否符合指定的正则表达式
  • 收集的ASP.NET中常用正则表达式
  • ASP.NET 中的正则表达式

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 ASP.NET ASP.NET MVC使用正则表达式验证手机号码 https://www.u-9.cn/biancheng/aspnet/94165.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务