Verify the mobile phone number in ASP. net mvc, asp. netmvc
In ASP. net mvc, you can use the RegularExpression feature to verify the mobile phone number.
public class Customer
{[Required (ErrorMessage = "Required")]
[Display (Name = "mobile phone number")]
[RegularExpression (@ "^ 1 [3458] [0-9] {9} $", ErrorMessage = "Incorrect Mobile Phone Number Format")] public string PhoneNumber { get; set; } }
In 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, JavaScript related to asynchronous verification must be referenced.
@Scripts.Render("~/bundles/jquery")@Scripts.Render("~/bundles/jqueryval")
In 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 = "submit"/>
}