Page_ClientValidate checks several times.

the problem is that the final check message (warning) is displayed twice. I can not understand the reason.

Please, help. Here is the code

function validate() //javascript function { if (typeof(Page_ClientValidate) == 'function') { var isPageValid = Page_ClientValidate(); if(isPageValid) { } } } <asp:Button ID="btn1" runat="server" OnClientClick="validate()" Text="button" ValidationGroup="ContactGroup" /> <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" ShowMessageBox="true" ShowSummary="false" ValidationGroup="ContactGroup" /> 
+16
javascript validation
Jun 09 '09 at 11:20
source share
8 answers

The problem is that the Page_ClientValidate function takes an input parameter, if you do not specify an input, then validationsummary is run once for the group name.

In your case, the trigger function is twice: once for groupname = "ContactGroup" and another time for groupname = ""

you have to change

 var isPageValid = Page_ClientValidate(); 

to

 var isPageValid = Page_ClientValidate(''); 

if you do not want to specify a ValidationGroup, or if you want to specify a group name, you need to call Page_ClientValidate as follows:

 var isPageValid = Page_ClientValidate('ContactGroup'); 
+23
Jun 17 '10 at 2:52 p.m.
source share

Hey. First of all, you have to release ValidationGroup = "ContactGroup" from the button, because in this case the verification group will first trigger the validation on the page, then the OnClientClick event containing the validation function that will trigger the validation of the page again.

Then you must pass the ContactGroup verification group to the Page_ClientValidate () function so that it knows which controls to check, because just calling Page_ClientValidate () will check all the controls regardless of their verification group (and can display more than one verification message times, depending on how many validation groups you have).

In short, do something like this:

 function validate() //javascript function { if (typeof(Page_ClientValidate) == 'function') { var isPageValid = Page_ClientValidate('ContactGroup'); if(isPageValid) { //your custom code } } } <asp:textbox id="txtMyBox" runat="server"/> <asp:requiredFieldValidator Id="rfv1" runat="server" ControlToValidate="txtMyBox" ValidationGroup="ContactGroup" ErrorMessage="Bad!"/> <asp:Button ID="btn1" runat="server" OnClientClick="validate()" Text="button"/> <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" ShowMessageBox="true" ShowSummary="false" ValidationGroup="ContactGroup" /> 
+5
Jan 18 '10 at 15:00
source share

just return false from the function and change the OnClientClick as shown below:

 <asp:Button ID="btn1" runat="server" OnClientClick="return validate();" Text="button" ValidationGroup="ContactGroup" /> function validate() //javascript function { if (typeof(Page_ClientValidate) == 'function') { var isPageValid = Page_ClientValidate(); if(isPageValid) { } } return false; } 
+3
Jan 26
source share

There is no need to manually call the Page_ClientValidate function if you do not want to perform validation outside of the postback attempt.

Set the CausesValidation buttons to true . This will run a check.

+2
Jun 09 '09 at 11:27
source share

You can do validation without showing messages, use the following code segment, and then use the isPageValid variable:

  if (Page_ValidationSummaries && Page_ValidationSummaries[0] && Page_ValidationSummaries[0].showmessagebox) { var showMessagesOption = Page_ValidationSummaries[0].showmessagebox; Page_ValidationSummaries[0].showmessagebox = "False"; isPageValid = Page_ClientValidate(); Page_ValidationSummaries[0].showmessagebox = showMessagesOption; } 
+1
Sep 28 '11 at 9:50 a.m.
source share

Remove the button click event, which forces me to check the second check.

0
Jun 09 '09 at 11:24
source share

remove the onclientclick button event, no need for this

0
Jun 09 '09 at 11:25
source share

I know this is an old post, but here is a solution that can be more flexible. Like other user suggestions, this decision is made by a validation group, which is checked by default with asp.net validation controls. This way you will not need to add OnClientClick="validate()" to the Button control.

 //Make sure the Page_ClientValidate function exists if (typeof (Page_ClientValidate) == "function") { //Stash the old implementation in a temp variable Page_ClientValidateOld = Page_ClientValidate; //Create a new implementation and store it //in Page_ClientValidate. Callers will now get //this implementation. Page_ClientValidate = function (validationGroup) { var isValid; //Call the old implementation firstโ€ฆ isValid = Page_ClientValidateOld(validationGroup); //and then call our extension if (!isValid) { // Do something } return isValid; } } 

If you want to know more about this approach, I recommend you take a look at this blog post: http://hyperthink.net/blog/interception-patterns-in-javascript/

0
May 28 '14 at 10:16
source share



All Articles