Verification of mobile phone numbers in ASP.

Source: Internet
Author: User

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.

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.