In-depth introduction to ASP + verification (III)

Source: Internet
Author: User

Client Verification

If client verification is enabled on your page, a completely different event sequence occurs during the round-trip process. Client verification uses the client JScript®. This verification does not require any binary components.

Although JScript Language Standardization is well done, there is no widely used Document Object Model (DOM) used to interact with HTML documents in browsers. Therefore, client verification is only performed in Internet Explorer 4.0 and later versions, because the object to be verified is Internet Explorer DOM.

From the server perspective, client-side authentication only means that the verification control sends different content to HTML. In addition, the event sequence is identical. The server check is still executed. Although it seems redundant, it is very important because:

Some validation controls may not support client scripts. There is a good example: if you want to use CustomValidator and the server verification function at the same time, but there is no client verification function.
Security considerations. Some people can easily get a page containing scripts, and then disable or change the page. You should not use your scripts to prevent bad data from entering your system, but only to get faster feedback from users. Therefore, if you want to use CustomValidator, you should not provide a client verification function without the corresponding server verification function.
Each verification control ensures that a standard client script block is sent to the page. In fact, this is only a small part of the Code, including references to the Code in WebUIValidation. js in the script library. The script library file contains all the logic verified by the client. The file must be downloaded separately and can be stored in the browser cache.

About script Library

Because the Web Control script is verified in the script library, it is not necessary to directly send all the client-side verification code to the page, even though it seems like this. The main script file references are as follows:

<Script language = "javascript"
Src = "/_ aspx/1.0.9999/script/WebUIValidation. js"> </script>

By default, the script file is installed in the default root directory of the "_ aspx" directory and called using the include command relative to the root script. This command starts with a forward slash. This reference indicates that each individual object does not need to contain a script library. All pages on the same computer can reference the same file. You will notice that there is also a common language runtime version in this path, so that different runtime versions can run on the same computer.

If you check your default virtual root directory, you will find the file and view its content. The location of these files is specified in the config. web file. The config. web file is an XML file used for most ASP + settings. The location definition in the file is as follows:

<Webcontrols
Clientscriptslocation = "/_ aspx/{0}/script /"
/>

Read this script to learn more about events. However, we recommend that you do not modify these scripts because their functions are closely linked to specific runtime versions. When the version is updated during running, these scripts may also need to be updated. You will or will not change the version, or you will face the problem that the script does not work. If the scripts must be changed for a specific project, back up the scripts and point your project to the backup files by using the private config. web file to replace the files. If the string contains the format instruction "{0}", the running version will replace the instruction. It is best to change the location to a relative or absolute reference.

Disable client Verification

Sometimes you may not want to perform client verification. If the number of input fields is small, client verification may be of little use. After all, you need a logic that requires one round-trip to the server every time. You will find that the dynamic information on the client has a negative impact on your layout.

To disable Client verification, use the Page command "clienttarget = downlevel ". This command is similar to the beginning of the following ASPX file:

<% @ Page Language = "c #" clienttarget = downlevel %>

The default value of this command is "auto", indicating that you only perform client verification for Microsoft Internet Explorer 4.0 or later.

Note: Unfortunately, in Beta 1, this command does not only disable verification, but also enables all Web controls to be processed using HTML 3.2 tags, which may produce unexpected results. The final version provides a better way to control this issue.

Client Event Sequence

This sequence is an event sequence that occurs when a page containing client verification is run:

When loading a browser on a page, you need to initialize each verification control. These controls are sent as <span> tags, and their HTML features are the closest to those on the server. Most importantly, all the input elements referenced by the validators are "mounted ". The referenced input element modifies its client events so that verification routines can be called each time a change is input.
The code in the script library will be executed when you use the tab key to switch between fields. When an independent field is changed, the verification conditions are re-evaluated to make the validators visible or invisible as needed.
When you try to submit a form, all validators will be re-evaluated. If all the validators are valid, the form is submitted to the server. If one or more errors exist, the following situations may occur:
The submission is canceled. The form is not submitted to the server.
All invalid validators can be seen.
If a verification digest contains ShowSummary = true, all errors from the validation control are collected and their content is updated with these errors.
If a verification digest contains ShowMessageBox = true, errors are collected and displayed in the client information box.
Because client-side verification controls are executed each time you enter a change or submit, these verification controls are usually evaluated twice or more on the client. Note that after submission, these verification controls will still be re-evaluated on the server.

Client API

There is a small API that can be used on the client to achieve various effects in your own client code. Because some routines cannot be hidden, in theory, you can use the client to verify all variables, features, and functions defined by the script. However, many of them are implementation details that can be changed. The following summarizes the client objects we encourage you to use.

Table 3. client objects

Name type description
The Page_IsValid Boolean variable indicates whether the page is currently valid. The verification script always keeps the variable up-to-date.
Page_Validators element array this is an array containing all validators on the page.
The Page_ValidationActive Boolean variable indicates whether verification is required. If this variable is set to False, you can disable verification by programming.
Isvalid Boolean attribute each client validators has this attribute, indicating whether the validators are currently valid. Note that in the PDC version, this attribute is case-insensitive ("IsValid ").


Bypass client Verification

You often need to add the "cancel" button or navigation button on the page to execute a task. In this case, you may want to use this button to submit the page even if an error occurs on the page. Because the client button "onclick" event occurs before the form's "onsubmit" event, it may avoid submitting the check and bypassing the verification. The following describes how to use the HTML Image control as the "cancel" button to complete the task:

<Input type = image runat = server
Value = "cancel"
Onclick = "Page_ValidationActive = false ;"
OnServerClick = canccancel_click>

Using the Button or ImageButton control to execute this task may cause some confusion, because the "onclick" event is assumed to be a server-side event with the same name. You should set this event in the client script:

<Asp: ImageButton runat = server id = cmdImgCancel
AlternateText = "cancel"
OnClick = canccancel_click/>

<Script language = "javascript">
Document. all ["cmdImgCancel"]. onclick =
New Function ("Page_ValidationActive = false ;");
</Script>

Another way to solve this problem is to set the "cancel" button so that it does not trigger the submit event in the client script when returning. This is an example of the HtmlInputButton and LinkButton controls.

Special effects

Another common requirement is that when an error occurs, in addition to the error information displayed by the validators themselves, other effects are required. In this case, any modifications you make must be made simultaneously on the server or client. Assume that you need to add a Label to change the color based on whether the input is valid. The following describes how to implement the task on the server:

Public class ChangeColorPage: Page {
Public Label lblZip;
Public RegularExpressionValidator valZip;

Protected override void OnLoad (EventArgs e ){
LblZip. ForeColor = valZip. IsValid? Color. Black: Color. Red;
}
}

All of the above methods are perfect. However, as long as you modify the verification above, you will find that unless you perform the same operation on the client, it will look very different. The verification framework will avoid many of these dual effects, but it cannot avoid other effects that must be achieved simultaneously on the client and server. The following is a snippet for executing the same task on the client:

<Asp: Label id = lblZip runat = server
Text = "Zip Code:"/>
<Asp: TextBox id = txtZip runat = server
OnChange = "txtZipOnChange ();"/> </asp: TextBox> <br>
<Asp: RegularExpressionValidator id = valZip runat = server
ControlToValidate = txtZip
ErrorMessage = "invalid zip code"
ValidationExpression = "[0-9] {5}"/> <br>

<Script language = javascript>
Function txtZipOnChange (){
// If the client verification is not active, no operation is performed.
If (typeof (Page_Validators) = "undefined") return;
// Change the label color
LblZip. style. color = valZip. isvalid? "Black": "Red ";
}
</Script>

Beta 1 client API

For Beta 1, some functions that can be called from client scripts may cause other situations.

Table 4. Functions called from client scripts

Description
ValidatorValidate (val) uses a client validator as the input. Enable the validator to check its input and update its display.
ValidatorEnable (val, enable) gets a client validator and a Boolean value. Enable or disable the client validator. If disabled, the client validators are not evaluated and always displayed as valid.
ValidatorHookupControl (control, val) obtains an input HTML element and a client authenticator. Modify or create a change event for this element to update the validators during the change. This function is suitable

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.