3

Here is my html

<form name="sales">
<div class="sale-type">
                            <p>Which would you like to sell?</p>
                            <fieldset class="md-form form-group">
                                <input ng-model="art.originalForSale" name="originalForSale" type="checkbox" class="filled-in" id="checkbox2">
                                <label for="checkbox2">My Original ART</label>
                            </fieldset>

                            <fieldset class="md-form form-group">
                                <input ng-model="art.printForSale" name="printForSale" type="checkbox" class="filled-in" id="checkbox22">
                                <label for="checkbox22">Prints of my ART</label>
                            </fieldset>
                        </div>
<button type="submit" ng-disabled="sales.$invalid" class="btn-form btn-next c-sprite" alt="Submit"></button>
</form>

I want to display validation message that shows at least one check box required how can i do it

found a solution from this question How to require at least 1 checkbox for AngularJS Form Validation?

but it uses a script i want a solution without using scirpt

currently i am using

<span  ng-show="part1.originalForSale.$error.required" class="error">Checkbox required</span>

4 Answers 4

1

I think that solve your problem:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<ng-form name="sales" ng-app >
  <div class="sale-type">
    <p>Which would you like to sell?</p>
    <fieldset class="md-form form-group">
      <input ng-model="art.originalForSale"  value="cb1" name="originalForSale" type="checkbox" class="filled-in" id="checkbox2" ng-required="!art.printForSale">
      <label for="checkbox2">My Original ART</label>
    </fieldset>

    <fieldset class="md-form form-group">
      <input ng-model="art.printForSale" value="cb2" name="printForSale" type="checkbox" class="filled-in" id="checkbox22" ng-required="!art.originalForSale">
      <label for="checkbox22">Prints of my ART</label>
    </fieldset>
  </div>
  <span  ng-show="sales.$invalid" class="error">Checkbox required</span>
  <button type="submit" ng-disabled="sales.$invalid" class="btn-form btn-next c-sprite" alt="Submit"></button>
</ng-form>

Sign up to request clarification or add additional context in comments.

2 Comments

Strange!! its not working the error message still showing
When I run the code, the message is showing only if the both checkbox is unmarked.
0

You can use ngRequired directive. There is a simple example.

<input name="checkbox1" type="checkbox" ng-model="isChecked1" ng-required="isChecked2">
<input name="checkbox2" type="checkbox" ng-model="isChecked2" ng-required="isChecked1">

You can use ngMessages for validation message

<div ng-messages="myForm.checkbox1.$error || myForm.checkbox2.$error" style="color:maroon" role="alert">
    <div ng-message="required">You did not select a checkbox !</div>
</div>

4 Comments

It didn't worked always shows the error message even if checkbox checked
<span ng-show="part1.originalForSale.$invalid || part1.printForSale.$invalid" class="error">Atleast one checkbox required</span>
This is not what i wrote. Use same directives and $error variable.
0

Have you tried below?

<span  ng-show="!(art.printForSale || art.originalForSale) " class="error">Checkbox required</span>

Use this with $dirty if you want to show the message after submit form.

Comments

-1

write required attributes in input tag for showing msg

    <span ng-show="frmRegister.name.$dirty && frmRegister.name.$error.">this is req...</span>

1 Comment

I need to check either one of the field

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.