Use Javascript to implement. NET verification controls

Source: Internet
Author: User

We often encounter this problem: when we select checkbox, we want to verify a control, cancel the selection, hide and cancel the verification. If you fully use server events to implement this function, the user will certainly be poor, so we can use javascript to implement the. net verification control function.

First, let's take a look at what the. NET verification control does on the page.

 
 
  1. <asp:TextBoxIDasp:TextBoxID="txtValidator"runat="server"></asp:TextBox> 
  2. <asp:RequiredFieldValidatorIDasp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"  
  3. ControlToValidate="txtValidator"ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator> 

Analyzed on the page as follows:

 
 
  1. <inputnameinputname="txtValidator"type="text"id="Text1"/> 
  2. <spanidspanid="Span1"style="color:Red;visibility:hidden;">RequiredFieldValidator</span> 

The verification control is changed to a hidden span on the page and displayed when verification is required.

At the same time, there are some steps for verification on the page

 
 
  1. <ScripttypeScripttype="Text/javascript"> 
  2. //<! [CDATA [ 
  3. // Set of verification controls. When a verification control is added, an item is added. 
  4. VarPage_Validators = newArray (document. getElementById ("RequiredFieldValidator1 ")); 
  5. //]> 
  6. </Script> 
  7. <ScripttypeScripttype="Text/javascript"> 
  8. //<! [CDATA [ 
  9. VarRequiredFieldValidator1 = document. all? Document. all ["RequiredFieldValidator1"]: document. getElementById ("RequiredFieldValidator1"); // locate the verification control 
  10. RequiredFieldValidator1.controltovalidate = "txtValidator"; // bind the control to be verified 
  11. RequiredFieldValidator1.errormessage = "RequiredFieldValidator"; // displays error messages that fail Verification 
  12. RequiredFieldValidator1.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid"; // Method for verification 
  13. RequiredFieldValidator1.initialvalue = ""; 
  14. //]> 
  15. </Script> 
  16. <ScripttypeScripttype="Text/javascript"> 
  17. //<! [CDATA [ 
  18. VarPage_ValidationActive = false; 
  19. If (typeof (ValidatorOnLoad) = "function "){ 
  20. ValidatorOnLoad (); 
  21. FunctionValidatorOnSubmit (){ 
  22. If (Page_ValidationActive ){ 
  23. ReturnValidatorCommonOnSubmit (); 
  24. Else { 
  25. Returntrue; 
  26. //]> 
  27. </Script> 

We construct similar scripts for control verification.

Add verification span

 
 
  1. <Asp: CheckBoxIDAsp: CheckBoxID= "CbSelect"Runat= "Server"Text= "Select"Onclick= "AjusSelectValidator (this, 'spantxtusername ')"/> 
  2. <Asp: TextBoxIDAsp: TextBoxID= "TxtUserName"Runat= "Server"></Asp: TextBox> 
  3. <SpanidSpanid= "SpantxtUserName"Style= "Color: Red; display: none ;">*

This item cannot be blank </span> // Add a span to display verification information

Add script

 
 
  1. <ScriptlanguageScriptlanguage= "Javascript"Type= "Text/javascript"> 
  2. VarIsSelectID='<% = CbSelect. ClientID %>';
  3. VartxtUserName='<% = TxtUserName. ClientID %>';
  4. FunctionajusSelectValidator (checkbox, validatorID ){
  5. AjustValidator (checkbox, validatorID, txtUserName );
  6. }
  7. </Script> 
  8. // Validator. Js
  9. FunctionajustValidator (checkbox, validatorID, controltoHideID ){
  10. If (checkbox. checked ){
  11. Register (validatorID, controltoHideID );
  12. Document. getElementById (controltoHideID). Style. visibility="Visible";
  13. }
  14. Else {
  15. RemoveValidator (validatorID );
  16. Document. getElementById (controltoHideID). Style. visibility="Hidden";
  17. }
  18. }
  19. // Add verification Association
  20. Functionregister (validatorID, controltoValidateId ){
  21. Page_Validators.push (document. getElementById (validatorID ));
  22. Varspan=Document. GetElementById (validatorID );
  23. SpanTest. controltovalidate=ControltoValidateId;
  24.  
  25. Span. errormessage="* This item cannot be blank";
  26. Span. display="Dynamic";
  27. Span. evaluationfunction="RequiredFieldValidatorEvaluateIsValid";
  28. Span. initialvalue="";
  29. //SpanTest. style. visibility="Visible";
  30. ValidatorOnLoad ();
  31. }
  32. // Cancel the authentication Association
  33. FunctionremoveValidator (validatorID ){
  34. Varvalidator=Document. GetElementById (validatorID );
  35. If (validator ){
  36. For (Vari=0; I<Page_Validators.length; I ++ ){
  37. If (Page_Validators [I] = validator ){
  38. Page_Validators.splice (I, 1 );
  39. I --;
  40. }
  41. }
  42. Validator. style. display="None";
  43. ValidatorOnLoad ();
  44. }
  45. }

PS: If the control is not verified on the page, an error occurs when you directly execute the code above. Because Page_Validators is null, push directly to the control and throw an exception. Therefore, you must add a verification control when using it.

  1. JQuery calls the WCF Service to pass JSON objects
  2. Comparison of the performance of JavaScript-parsed Json strings in various browsers
  3. Services_JSON version 1.0.0 released
  4. Use JSONP to solve cross-Origin data access problems
  5. Data conversion between JSON and JAVA

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.