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 PhoneNumber {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= "Submit" value= "Submission"/>
}
Verification of mobile phone numbers in ASP.