views:

2018

answers:

6

problem i have is that, the validation summary message(alert) is displayed twice. I cannot figure out 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" />
A: 

Remove the click event of the button, that forces second validation I think.

Canavar
A: 

remove the onclientclick event of button there is no need for that

Meetu Choudhary
+2  A: 

There is no need to manually call the Page_ClientValidate function, unless you're wanting to do the validation outside of a postback attempt.

Set the buttons CausesValidation to true. That'll run the validation.

Slace
+1  A: 

Hey there.First of all you should loose the ValidationGroup="ContactGroup" from the button because having validation group in it will first call the validation on the page then the OnClientClick event that contains the validate function which will call the page validation once again.

Then you should pass the validation group "ContactGroup" to the Page_ClientValidate() function so it knows which controls to validate because simply calling Page_ClientValidate() will validate all controls regardless of their validation group(and it may display the validation message more than once, 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" />
TestSubject09
+3  A: 

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;

}

+1  A: 

The problem is that the function Page_ClientValidate takes an input parameter, if you don't specify the input then the validationsummary triggers once per groupname.

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

you should change

var isPageValid = Page_ClientValidate();

to

var isPageValid = Page_ClientValidate('');

if you don't want to specify a ValidationGroup, or if you want to specify a groupname then you need to call Page_ClientValidate like so:

var isPageValid = Page_ClientValidate('ContactGroup');