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 routine cannot be hidden, in theory, you can use the client to verify all variables, characteristics, and functions defined by the client. However, many of them can be changed. The following summarizes the client object we encourage you to use.
Table 3. Client object
Name type description
Page_isValid Boolean variable pointed out whether the page is currently valid. Verifying scripts always keep the variable latest.
Page_validator element array This is an array containing all the verifications on the page.
Page_validationActive Boolean variables indicate whether it should be verified. Set this variable to FALSE can be proofed by programming.
ISVALID BOOLEN attributes each client verification device has the property, pointing out whether the verification device is currently valid. Please note that in the PDC version, this attribute is mixed with Isvalid.
Bypassing client verification
One task you often need to execute is to add the "Cancel" button or navigation button on the page. In this case, even if there are errors on the page, you may also want to use the button to submit the page. Because the client button onClick event occurs before the onSubmit event of the form, it may avoid submitting inspection and bypassing verification. The following shows 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;
onServerClight = cmdcancel_click>
Use the Button or ImageButton control to execute some confusion, because the OnClick event assumes that it is 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 = cmdcancel_click/>
<script language = javascript>
document.all [cmdimgcancel] .onClick =
new function (page_validationActive = false;);
</script>
Another method to solve this problem is to set a certain settings of the "Cancel" button so that it will not trigger the submission event in the client script when returning. HTMLINPUTBUTTON and LinkButton controls are the examples.
Special effect
Another common requirement is that in addition to error information displayed by the verification device itself, some other effects are needed. In this case, any modification you make needs to be carried out at the same time on the server or client. Suppose you need to add a label to change the color according to whether the input is valid. The following is how to implement this task on the server:
Public Class ChangecolorPage: Page {
public label lblzip;
public regulaxpressionvalidator value;
ProteCted Override Void Overoad (EventArgs E) {{
lblzip.Forecolor = valzip.isvalid? Color.black: color.red;
}
}
All the above methods are perfect, but 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 inconsistent. The verification framework will allow you to avoid many such dual effects, but it cannot avoid other effects you must achieve at the same time on the client and server. The following is a fragment that performs 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: RegulaRXPRESSIONVALIDIDATOR ID = VALZIP RUNAT = Server
Controltovalidate = txtzip
errorMessage = invalid postal code
ValidationXpression = [0-9] {5} /> <br>
<script language = javascript>
Function txtziponchange () {{) {
// If the client verification is not in the activity, it will not perform any operation
ifof (page_validators) == Undefined) Return;
// Change the color of the label
lblzip.style.color = valzip.isvalid? Black: red;
}
</script>
Beta 1 client API
For Beta 1 edition, some functions that can be called from the client script will cause other situations.
Table 4. Function from client script call
Name Description
Validatorvalidate (Val) uses a client verification device as input. Make the verification device check its input and update its display.
ValidatoreNable (Val, Enable) obtain a client verification device and a Boolean value. Enable or disable the client verification device. If it is disabled, the client verification device will not be evaluated, and the client validator will always be valid.
ValidatorHookupControl (Control, Val) obtained an input HTML element and a client verification device. Modify or create the Change event of the element so that the verification device can be updated during the change. This function is suitable for customized verifications based on multiple input values.
Its special purpose is to enable or disable the verification device. If you want to verify that you only take effect in specific circumstances, you may need to change the activation status at the same time on the server and clients, otherwise you will find that the user cannot submit the page.
The following is the above example plus a field. This field will be verified only when the selection of a check box is canceled.
Public Class Conditional: Page {
public htmlinputcheckbox chksameas;
public researchFieldValidator rfvalshipaddress;
Protected Override Void Validate () {) {)
BOOL Enableship =! Chksameas.checked;
rfvalshipaddress.enabled = enableship;
base.validate ();
}
}
The following is the code of the client equivalent:
<input type = checkbox runat = server id = chksameas
Onclick = Onchangesameas ();> The same as the payment address <br>
<script language = javascript>
Function onchangesameas () {
var entlesHip =! Event.srcelement.status;
Validatorenable (RFValshipaddress, Enableship);
}
</script>
Effective rules and useful error information
Each verification device shows specific error information about specific conditions in specific controls. There are some rules that confirm whether it is valid. At first, you may be confused as a developer, but if you want to generate error information with actual help to users, these rules are necessary.
All empty verifications (except for RequiredFieldValidator) are considered valid. If an empty value is invalid, you usually need a RequiredFieldValidator and an other verificationer. You need to do this, because in general, you always want to show different error information on the empty verification device and effectiveness. You can also use unclear information, such as "you must enter a value, and this value must be between 1 and 10".
Another special rules used when the input field cannot be converted into a specified data type is related to compareValidator and Rangevalidator. The validity assessment process of the compareValidator of ControltocomPare specifies the validity assessment process is as described below:
If the input field referenced by Controltovalidate is empty, it is effective.
If the input field referenced by Controltovalidate cannot be converted to the required data type, it is invalid.
If the input field referenced by Controltocompare cannot be converted to the required data type, it is valid.
The input field is converted to the required data type and compares.
The third step looks a little inconsistent. The reason for this is because if the verification device checks the effectiveness of multiple fields at the same time, it is difficult to write meaningful error information for the verification device. A independent verification device should be used to report the error situation in the ControltocomPare input field. Rangevalidator has similar working methods, with Maximum and Minimum properties.
ENabled, Visible and Display Properties' functions
The difference between the Enabled, Visible and Display properties of the verification device may not be very obvious.
Display = None can be used to specify that the verification device does not directly display any content, but still evaluates, still affects the overall effectiveness, and can still put errors in the summary of the client and server. For client verification, these values are determined to use the visible style characteristics or use the display style characteristics to open or close the verification device. For server -side verification, Display = Dynamic means that the input does not display any content when the input is valid, and Display = Static represents a space that does not change (). The last setting is to be folded into no content when the cell that contains only the verification device in the table is valid.
Why not only use Visible = false to make the verification device visible? In ASP+, the Visible attribute of the control has many meanings: the control of Visible = FALSE will not be processed or displayed at all. It is precisely because of this meaning that the Visible = FALSE of the verification device means that not only does not display any content, but also cannot be used. This verification device will not be evaluated, will not affect the validity of the page, nor will it be put in the abstract.
Enabled is neutral. In most cases, the effect of enabled = false and visible = false is exactly the same. In the Beta 1 edition or higher version, there is an important difference: in client verification, the disabled verification device will still be sent to the browser, but it is in a disabled state. You can use the ValidatoreNable function in the client script to activate the verification device.
When using Visible or Enabled to control whether to verify, pay attention to the order order on the above server. Or change before verification, or re -verify after the change. Otherwise, their ISVALID values will not reflect changes to attributes.
CustomValidator control
The easiest way to extend the verification framework is to use the CustomValidator control. This control can be used to perform verifications that other verification controls cannot be performed, but they can also execute verifications that need to access information on the server (such as databases or web services).
If the CustomValidator with only one server verification function is added, you will notice that the verification device does not participate in client verification. When the user switches between each field with the Tab key, the CustomValidator will not be updated, and the round -trip server needs to perform its verification at one time. If you want to use the CustomValidator to perform a check that does not require any information on the server, you can also use the clientValidationFunction property to make the verification device completely participate in client verification. Suppose you provide a ClientValidationFunction. Ideally, you should perform exactly the same inspection as the server verification processing process. But in fact, it is only a part of the verification. The verification of the client verification function does not exceed the verification of executing on the server because hackers can easily bypass the verification function.
The following is a simple example of using CustomValidator on the client and server, only check whether the input is even. Let's introduce the server function (in C#):
{partialive service) {position
try {
int i = int.fromstring (value);
Return ((i % 2) == 0);
} Catch {
Return false;
}
}
The following is the declaration method of the function on the client, and a client verification function that performs the same check. This is usually a JScript form, but if your goal is Microsoft® Internet Explorer, you can also use VBScript® form.
<ASP: CustomValidator ID = CustomVal2 Runat = Server
ErrorMessage = numbers cannot be removed!
Controltovalidate = txtCustomdata
onSerValIdationFunction = Servervalidation
clientValidationFunction = Checkeven /> <br>
Data Field: <Asp: Textbox ID = TXTCUSTOSDATA RUNAT = Server />
<script language = javascript>
<!--
Function Checkeven (Source, Value) {{
var value = PARSEINT (VALUE, 10);
if (ISNAN (Val))
Return false;
Return ((Val % 2) == 0);
}
//->
</script>
Here are some precautions using CustomValidator:
Similar to all other verification controls (except for RequiredFieldValidator), if the input field is empty, it is considered that the CustomValidator is effective.
If the older browser is used or the client verification is closed, the client verification function cannot be called. Before defining the function, you don't have to check the function of the browser used in the browser, but you need to ensure that the browser does not cause script errors due to definition. Be sure to make your client code as an annotation of HTML, as shown in the following example.
Two parameters are passed to your client function and correspond to the parameters passed to the server function. The first is the client verification device element, and the second is the control value specified by the controltovalidate. However, on the client, you can choose not to define parameters for the function, which will work normally.
If you use Beta1 or higher versions, you can keep Controltovalidate as empty. In this mode, the server function will always trigger a round -trip round trip, and the client function will always be triggered every time you try to submit it. You can use this feature to verify the controls that other methods cannot verify, such as CheckBoxList or separate radio buttons. If the condition is based on multiple controls, and you do not want the user to evaluate the condition when switching between each field on the page, you can use this method.
Another option in Beta 1 or higher version is the Change event of multiple controls. The method is to add some embedded scripts that call the client function ValidatorHookupControl, as described above.
Which controls can be verified?
To enable the control to be verified by the control reference, the control must have verified attributes. All verified controls have validationPropertyattribute properties, which indicate the attribute that should be read during verification. If you write your own control, you can specify the attributes to be used by providing one of them, so that the control is involved in verification.
To enable the verification to be carried out normally on the client, the attribute must correspond to the value characteristics of the HTML element displayed by the client. Many complicated controls (such as DataGrid and Calendar) are not worth the client and can only be verified on the server. Therefore, only controls closest to HTML elements can participate in verification. In addition, control must have a single logic value on the client. Therefore, RadiobuttonList can be verified, but CheckBoxList cannot.
End
The above -mentioned explanation of ASP+ verification may have exceeded the content you want to understand. Enjoy it!