ASP. NET and JavaScript easily implement input information verification

Source: Internet
Author: User

We usually enter some data, such as ID card numbers, email accounts, numbers, Chinese characters, and combination of numbers, whether we access the Internet, send emails, work, study, or live, or the input data that meets a certain specification is what we want, and the results are often unsatisfactory. We want the user to input numbers, but he just needs to input Chinese characters, therefore, an unknown error may occur in the software and the efficiency of using the system may be reduced. How can this problem be solved?

This requires a set of verification controls, which are commonly used and convenient to provide us with verification functions. Let's take a look at how they are used.

Verify controls
Verify the control name Verification content
Requirefieldvalidator Verify that the entered content is empty
Rangevalidator Check that the entered information is in a certain range
Regularexpressionvalidator Check input information. Otherwise, the regular expression is used.
Comparevalidator The input value is compared with a constant or control.
Customvalidator Custom verification rules for the server or client
Validationsummary Displays a summary of verification errors for the verification control.

Verify that the control has the same properties
Attribute Description
Controltovalidate Set to the Control ID to be verified. If no data exists, the string in errormessage is displayed.
Errormessage Indicates the error message when the check is invalid.

Of course, each control has its own special attributes based on its different functions. Let's take a look at their own special attributes.

[Requirefieldvalidator]

Display: Display Mode of the error message. Static indicates that the error message of the control occupies a fixed position on the page. If there is no error, the display is similar to the label control; dynamic indicates that the page space is occupied when the control error message appears; none indicates that the prompt information is not displayed when the error occurs, but it can be displayed in validationsummary.

Text: If the display is static, the text is displayed when no error occurs.

Code:

<Tr> <TD> <asp: Label id = "label1" runat = "server" text = "username:"> </ASP: label> </TD> <asp: textbox id = "txtusername" runat = "server"> </ASP: textbox> <asp: requiredfieldvalidator id = "require" controltovalidate = "txtusername" display = "dynamic" runat = "server" errormessage = "no user name entered! Enter! "> </ASP: requiredfieldvalidator> </TD> </tr>

When this control is used, if it fails to be verified, other verification controls will not work. It can be said that this is the prerequisite for the verification to pass. As for why is this checked, if the errormessage attribute is set, once the input box is blank, no matter where you click the page, there will be an error message, And, page. the value of the isvalid attribute can only be set to true, but cannot be set to false. If the input box is empty, the page cannot be refreshed and cannot take effect, I feel that this can be used for verification purposes. When an input error occurs, the page is not refreshed, which may reduce the burden on the server, but it is unfriendly.

[Rangevalidator]

Maximumvalue: maximum value of the range.

Minimumvalue: the minimum value of the range.

Text: If the display is static, the text is displayed when no error occurs.

Type: defines the type of the input value of the control, such as string, integer, double, data, and currency. The input value is converted before verification.

[Regularexpressionvalidator]

Validationexpression: the specified regular expression.

Code:

<Tr> <TD> <asp: Label id = "label4" runat = "server" text = "enter email:"> </ASP: label> </TD> <asp: textbox id = "txtemail" runat = "server"> </ASP: textbox> <asp: regularexpressionvalidator id = "regularexpressionvalidator1" runat = "server" controltovalidate = "txtemail" errormessage = "Incorrect email format! "Validationexpression =" \ W + ([-+. '] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W +) * "> </ASP: regularexpressionvalidator> </TD> </tr>

 

[Comparevalidator]

Equal: verification is passed when the two values of the comparison are equal.

Notequal: when the two values are not equal, verification is passed.

Greaterthan: When the verified value is greater than the specified constant value or control value, it passes verification.

Greaterthanequal: When the verified value is greater than or equal to the verified constant value or control value, it passes verification.

Lessthan: When the verified value is smaller than the specified constant value or control value, it passes verification.

Lessthanequal: it is verified when the verified value is less than or equal to the verified constant value or control value.

Datatyecheck: When the verified value is the same as the specified constant value or control value type, it passes verification.

Controltocompare: ID of the control to be compared.

Valuetocompare: Specifies the common values to be compared.

Code:

<Tr> <TD> <asp: Label id = "label3" runat = "server" text = "Confirm Password:"> </ASP: label> </TD> <asp: textbox id = "txtpasswordfirm" runat = "server"> </ASP: textbox> <asp: comparevalidator id = "comparevalidator1" runat = "server" controltovalidate = "txtpasswordfirm" controltocompare = "txtpassword" errormessage = "two input passwords are inconsistent! "> </ASP: comparevalidator> </TD> </tr>

Input information verification is an essential function of every system or software. Security Verification is a prerequisite for enhancing the security and reliability of the system. Intelligent Input verification is our development trend. Based on the user's input intention, intelligent Input and smart prompt information make the software more convenient for users. For example, the data dictionary can automatically match user input information. The verification function is not only implemented by controls, some scripts, such as JavaScript, can be used to process client-side script verification to reduce the burden on servers. We need more intelligent verification methods.

JS Form validation: http://blog.163.com/qinyixin_0711/blog/static/139340070201071823916331/

 

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.