ASP. NET-how to: Verify ASP. NET Server controls by Mode

Source: Internet
Author: User

You can check whether user input matches the predefined mode, such as phone number, zip code, and email address. To perform this verification, you must use a regular expression.

1. Add the regularexpressionvalidator control to the page and set the following attributes:

Attribute

Description

Controltovalidate

The ID of the control being verified.

Errormessage, text, display

These attributes specify the text and location of errors to be displayed when verification fails.

 

2. Set the validationexpression attribute to a regular expression to set the mode to be compared.

To allow multiple valid modes, use the bar character (|) to separate expressions.

Note: In client verification, ecmascript (JavaScript) is used to calculate the regular expression. This is slightly different from the regular expression check on the server.

 

3. Add a test in ASP. NET webpage code to check the validity.

The following code example shows how to use the regularexpressionvalidator control to check whether a user has entered a valid U.S. zip code. The validators check two modes: a 5-digit number and a 5-digit number plus four additional digits.

ZIP: <asp: textbox id = "txtzip" runat = "server"> </ASP: textbox>

<Asp: regularexpressionvalidator

Id = "txtzip_validation" runat = "server"

Controltovalidate = "txtzip"

Errormessage = "enter a valid us zip code ."

Validationexpression = "/d {5} (-/d {4 })? ">

</ASP: regularexpressionvalidator>

 

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.