3

I need to disable the save button on my form using javascript, if the validation on the page fails. If they don't then it must be enabled used the following code.

Code:

<script language="javascript" type="text/javascript">
  function ValidatePage() 
    {
        if (Page_ClientValidate == true) 
        {

        }

        else 
        {            
            document.getElementById(buttonID).disabled = true;
            return false;
        }
}
</script>

<asp:Button ID="btnSave" CssClass="button" runat="server" Text="Save" 
  Width="61px" Height="29px" OnClientClick="ValidatePage();" onclick="btnSave_Click" />

2 Answers 2

4

You have to use ClientID of button, also return true when if statement is true

document.getElementById('<%= btnSave.ClientID %>').disabled = true;

You have using Page_ClientValidate as variable but it is function.

Change

if (Page_ClientValidate == true) 

To

if(Page_ClientValidate(""))

You are not using the returned value of ValidatingPage() in OnClientClick and as a result you will get postback anyway

Change

OnClientClick="ValidatePage();"

To

OnClientClick="return ValidatePage();"
Sign up to request clarification or add additional context in comments.

2 Comments

It disables the submit button even if the validations are okay, I used an alert it always goes into the else part of the function eevn if the validations are okay.
Thank you very much Adil, Thank you for your generosity.
1

You can use OnClientClick="return ValidatePage();"

Hope This help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.