in ASP. NET MVC, you can use the RegularExpression feature to verify your phone number.
Public class Customer
{
[Required (errormessage = " required ")]
[Display (Name = " phone number ")]
[RegularExpression (@ "^1[3458][0-9]{9}$", errormessage = " cell phone number is not formatted correctly ")]
Public string Get set; }
}
In the 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);
}
}
}
In views/shared/_layout.cshtml, you must refer to the relevant JS for asynchronous validation.
@Scripts. Render ("~/bundles/jquery")
@Scripts. Render ("~/bundles/jqueryval")
In the home/index.cshtml:
@model MvcApplication1.Models.Customer
@{
Viewbag.title = "Index";
Layout = "~/views/shared/_layout.cshtml";
}
@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= "submitvalue=" commit "/>
}
Verification of mobile phone numbers in ASP.