ali akhbarizadeh
ali akhbarizadeh

Reputation: 51

I want to disable a button when asp RequiredFieldValidator shows error

I want to disable the form submit button when asp:RequiredFieldValidator shows error please advise

Upvotes: 2

Views: 2085

Answers (4)

Clarice Bouwer
Clarice Bouwer

Reputation: 3811

Perhaps you can try something like this:

    function WebForm_OnSubmit() {
        if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) {
            //disable button here
            return false;
        }
        //enable button here
        return true;
    }

For more information about this function visit and understanding the ASP.NET Validation Library visit this post.

Alternatively, as @Nag suggested, a custom validator may also be able to accomplish this as you are able to define the client side JavaScript.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460158

You could use this javascripot function onchange of the control which triggers validation:

<asp:TextBox id="TextBox1" runat=server OnChange="txtValidate();" />
<asp:RequiredFieldValidator  id="validator1" runat="server"
   ControlToValidate="TextBox1" ...>

<script>
function txtValidate() {
   // trigger validation from clientside
   var val = <%= validator1.ClientID %>;
   if (val.isvalid == false) {
     document.getElementById('btnSubmit').disabled = true;
   }
}
</script> 

Upvotes: 1

Pankaj Agarwal
Pankaj Agarwal

Reputation: 11311

if(Page_ClientValidate("SomeValidationGroup") == false)
   document.getElementById("button1").disabled = true;

Upvotes: 1

Serge
Serge

Reputation: 6692

You should use validationgroup + requirevalidation then the button should not be clickable

Upvotes: -1

Related Questions