Form Validation and disabled button



In above problem i have three radio buttons which let the user pay balance,minimum amount and other
amount.When user clicks on third radio button it opens up a div with input where user can enter their
amount and this field has some validation. And it followed up with some validation on next fieldset.
I want to disable the submit button until the form is valid. But even if I choose balance or minimum amount payment options in radio button, the submit button remains disabled (waiting for form to be valid. Probably looking for validation, which is on third option). How can i avoid this ?


  <fieldset  class="form-group clearfix">
     <legend class="">Fieldset Title</legend>
        <ul class="vList">
             <input type="radio" class="" name="balancePayment" id="payBalance"  ng-model="pay"     
                     value="balanceAmount" />
              <label class="" for="payBalance"> Balance:$130 </label>
              <input type="radio" class="form__radio" name="balancePayment" id="payMinimum"  ng-model="pay" 
              <label class="" for="payMinimum"> Minimum Amount:$4 </label>
              <input type="radio" class="" name="balancePayment" id="payOther" ng-model="pay" 
              <label class="form__radioLabel" for="payOther"> Pay Different Amount </label>
          <div class="otherpayment" ng-show ="pay=='otherAmount'" ng-class="{
                            'has-error':  myform.otherAmountpay.$invalid && myform.otherAmountpay.$dirty,
                <span class="help-block" ng-show="myform.otherAmountpay.$error.required &&  
                      myform.otherAmountpay.$dirty">Please enter your payment</span>
                <input  id="Tel1" name="otherAmountpay" ng-model="otherAmountpay" 
                       class="form-control" type="number" required=""/>


It is because the otherAmountpay field is still invalid as it has been marked as required, even though not displayed angular will still validate it. So you need to make the “required” constraint conditional on your textbox based on when you need to validate it (only when “other pay” is selected). You can use ng-required. i.e ng-required="pay=='otherAmount'", set required field only when the option other amount is selected.

<input  id="Tel1" name="otherAmountpay" ng-model="otherAmountpay" 
                       class="form-control" type="number" ng-required="pay=='otherAmount'"/>


Answered By – PSL

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.