Verify the mobile phone number in ASP. net mvc, asp. netmvc

Source: Internet
Author: User

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"/>
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.