How to use validation controls in ASP.

Source: Internet
Author: User

Used to check for input values:RequiredFieldValidator (mandatory field validation)
Compare two inputs by setting: CompareValidator (compare validation)
Whether the input is in the specified range: RangeValidator (range validation)
Regular expression validation controls: RegularExpressionValidator (regular expression validation)
Custom validation Controls: CustomValidator (custom validation)
Summary Verification Results: ValidationSummary (verification summary)

1, RequiredFieldValidator (required field validation) use

When a control on a page requires data to be entered, RequiredFieldValidator works, the ControlToValidate property selects the control that needs to be validated, and the ErrorMessage property is the error message that is displayed when the checksum is not valid.
The standard code used by the RequiredFieldValidator control is as follows:

<Asp:requiredfieldvalidatorid= "Validator_name" runat< Span style= "color: #00ff;" >= "Server" controltovalidate= "The name of the control to check" errormessage= "error message" display= "static| dymatic| None ">/asp:requiredfieldvalidator>

In the standard code above:
ControlToValidate: Indicates the control ID to be checked;
ErrorMessage: Indicates the error message that occurs when the check is not valid;
Display: The way the error message is displayed; static indicates that the error message of the control occupies a positive position on the page; dymatic means that the page control is not occupied when the control error message appears, and none indicates that the error appears, but can be displayed in validatorsummary;
PLACEHOLDER: Indicates that when display is static, the error message occupies a "placeholder" so large page space;

Now, let's look at an example:

<Asp:textboxId= "Txtname"Runat= "Server"/>
<Asp:requiredfieldvalidatorId= "Validator1" runat= "Server "controltovalidate=" Txtname "
errormessage= "Name must be entered" = "Static" >< Span style= "FONT-SIZE:10PT;" >* name must be entered </asp:requiredfieldvalidator >

In the example above, check if the Txtname control has input, and if not, display the error message "Name must be entered".

Note: The code above and the other controls below are best placed in the form, unlike in ASP, where the form is best written like this:

other code
Thus, the form is executed on the server side, and the submission will be valid;
2. CompareValidator (compare validation) control

The CompareValidator control is used to compare data consistency between two input controls, and it can also be used to verify the data type of the content in the control: such as shaping, string, and so on. The ControlToCompare and ControlToValidate properties are used to set the two controls to compare.
Compare controls compare the input of two controls to program settings

The standard code for the CompareValidator control is as follows:

<Asp:comparevalidatorId= "validator_id"Runat= "Server"ControlToValidate= "Control ID to validate"ControlToCompare= "Control ID to compare"errormessage= "error message" type< Span style= "color: #00ff;" >= "String| integer| double| datetime| Currency "operator=" equal| notequal| greaterthan| greatertanequal| lessthan| lessthanequal| DataTypeCheck "< Span style= "color: #ff00;" >display= "static| dymatic| None "></asp:comparevalidator>

In the standard code above:
Type: Represents the data type of the control to be compared;
Operator: Represents a comparison operation, here, there are 7 ways to compare, thecontroltovalidate property must be on the left side of the comparison operator, theControlToCompare property is on the right, can be calculated effectively.

The following table shows the available actions.

DataTypeCheck checks whether the data type of the two controls is valid.

Equal checks whether two controls are equal to each other.

GreaterThan checks if one control is greater than another.

Greaterthanequal checks whether a control is greater than or equal to another control.

LessThan checks whether a control is smaller than another control.

Lessthanequal checks whether a control is less than or equal to another control.

NotEqual checks whether two controls are not equal to each other.
3. RangeValidator (range validation) control

The RangeValidator control can be used to determine whether the values entered by the user are within a particular range, and the properties MaximumValue and MinimumValue are used to set the maximum and minimum values for the range.
Verify that the input is within a certain range, and that the range is determined by MaximumValue and Minimunvlaue.

The standard code for the RangeValidator control is as follows:

<Asp:rangevalidatorId= "vaidator_id"Runat= "Server"ControlToValidate= "Control ID to validate"
Type= "string| integer| double| datetime| Currency "minimumvalue=" minimum " maximumvalue= "Maximum"
Span style= "FONT-SIZE:10PT;" >errormessage= "error message" display= "static| dymatic| None "></asp:rangevalidator>

In the above code:
Use the MinimumValue and MaximumValue to define the value of the control input, and type to determine the types of the control's input values.
4. Regularexpresionvalidator (regular expression) control

The RegularExpressionValidator control can tell if the user entered an expression correctly, such as a phone number, zip code, URL, etc., and ControlToValidate property selects the control to be validated. The ValidationExpression property writes the style of the expression that needs to be validated, and the following example is a code that verifies the ZIP codes.
The functionality of regular expression validation controls is very powerful, so let's look at the standard code first:

<Asp:regularexpressionvalidatorId= "validator_id"Runat= "Server" controltovalidatevalidationexpression= "Regular expression" errormessage= "error message" display= "static| dymatic| None ">
placeholder </asp:regularexpressionvalidator >

In the above standard code, ValidationExpression is the focus, now look at its construction:
In ValidationExpression, different characters represent different meanings:
Asterisk "*":expression, together with other expressions, represents an easy combination;
square brackets "[]":Use and define acceptable characters. [abc123] means that the control can only accept a,b,c,1,2,3 6 characters; [A-z] denotes any uppercase letter;
anti-set symbol "^":Used to define characters that are not acceptable. [^a-h] means that the control can be accepted except for a to H 8 characters;
curly braces "{}":Defines the number of characters that must be entered. {6} means you can enter only 6 characters, {6,} means you must enter more than 6, no limit, {2,6} means you must enter 2 to 6 characters, but curly brackets must be placed behind square brackets, for example, [A-z]{4} indicates that you must enter any character between 4 bits A and Z.
small dots. " ":Used to represent any character. For example. {3,6} indicates that 3 to 6 arbitrary characters are accepted.
Vertical Line "| ":The logical symbol used to represent "or". For example [1-9]{3,6}| [A-za-z] {3} indicates that 3 to 6 digits or 3 letters can be accepted. (The case can be different, OH)
parentheses "()":Used for chunking, similar to parentheses in numeric operations.
slash "\":If you want the acceptable characters to contain the above special characters. For example[09] 3 That represents the telephone area code that has the format of "(XXX)" entered.

Note that in the above expression, the quotation marks are not included;
Example: Regular expression: ". *[a-z]" means that any combination of characters beginning with a number is followed by an uppercase letter.
5. CustomValidator (custom validation) control

The CustomValidator control is used to perform user-defined validation, which can be either a server-side or a client, and the following code is an example of using the client to verify the ZIP code.
The control defines the authentication method with a custom function with the following standard code:

<Asp:customvalidatorId= "validator_id"Runat= "Server"ControlToValidate= "Control to validate"
onservervalidate= "Server segment validation function" clientvalitationfunction< Span style= "color: #00ff;" >= "Customer segment validation function" errormessage= "error message" display></asp:customvalidator >
</asp:customvalidator>

In the code above, the user must define a function to validate the input.

6. ValidationSummary (validation summary) control
Instead of validating the data entered in a Web form, the control collects all validation error messages on this page and can organize them to be displayed later. This control outputs all the validation errors in the page to a list that is set by the DisplayMode property. The standard code is as follows:

<asp:validationsummaryid= "validator_id" runat= "Server" headertext= "Header information" showsummary= "true| False "diaplaymode< Span style= "color: #00ff;" >= "list| Bulletlist| Singleparagraph "/>
in HTML. BulletList is equivalent to

  • in HTML, and Singleparegraph indicates that there is no separation between error messages.
    The following code displays the error message in a dialog box.
  • <asp:validationsummaryID= "validator_id"runat= "Server"HeaderText= " Validationsummaryname "showsummary=" False "showmessagebox=" True "/>

    The above 6 validation controls need to be supplemented with the following:
    The properties of the page can be clienttarget to set whether all validation controls will be validated on the client. As long as this property is set to uplevel, downlevel, all validation will only be performed on the server. By default, most browsers are verified on the client side, so I don't know if this property is the default uplevel. Of course, if you want to set a single or several validation controls, use the original enableclientscript.
    An additional Setfoucsonerror attribute is also added. is to move the focus to the control when something goes wrong. This will not allow the user to click on the button after the failure to see the error message and then in a daze. The other is that CustomValidator added the ValidateEmptyText property to allow user-defined validation controls to be validated when the value is empty.
    The ValidationGroup property specifies the group of controls that are validated during validation. Set the control you want to validate when you click on one button to the same group name, and the other button to verify that the setting is another name, so that only the desired validation occurs when a keystroke is clicked, not all validation.
    In some cases when a customer does not want to display text but a picture or sound, the value of the ErrorMessage property of the validation control can be an HTML string, such as Errormessage= ' ', which makes the page lively.

    The display of the validation control is set to none. Summaryvalidator showsummary=false,showmessagebox=true; You can pop up a message box like JavaScript.

    How to use validation controls 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.