0

I am using Angular js, in which i have a textbox and collection of checkbox. I want to check if atleast one checkbox is checked when the submit button is clicked. Below is the existing code:

<form name="mainForm" id="createForm" ng-submit="mainForm.$valid && add()" novalidate>
        <div ng-controller="testController" ng-init="init()">    

    <div>
                        <label>Name :</label>
                    </div>
                    <div>
                        <input type="text" maxlength="150" required ng-model="testName" name="testName" />
                    </div>
                </div>
                <span style="color:red" ng-show="submitted == true && mainForm.testName.$error.required">Name is required</span>
                <br />
            <div class="row">
                    <div>
                        <label>Delivery Method</label>
                    </div>
                    <div ng-repeat="method in deliveryMethods">
                        <input type="checkbox" id="{{method.id}}"
                               value="{{method.value}}" name="deliveryMethod[]" ng-model="method.selected"
                               ng-click="toggleSelection(method.value)" ng-required="value.length==0"> {{method.value}}
                    </div>
                </div>

    <input type="submit" value="Submit" ng-click="submitted=true"/>

How to achieve this in angular js? Thanks

7
  • do you want to disable button if no checkbox is checked? Commented Jan 2, 2017 at 16:39
  • Can you add the deliveryMethods array in the question please ? Commented Jan 2, 2017 at 16:59
  • You can refer my example here - plnkr.co/edit/iH0uzhoHUTPFXH0g8Hyk?p=preview Commented Jan 2, 2017 at 17:08
  • Looks like it works. I get an error unless I check one of the checkboxes. What are you trying to do? Commented Jan 2, 2017 at 17:19
  • Hi Sumit, the demo works, however, i do not want to show the error message on checkbox unchecked.. The message should be shown only on submit button click event. Initially in demo, the message is not shown. without selecting any checkbox, i click submit, i get error message. this is correct. Now I check one checkbox, and uncheck it. Now thwe error message shows. can the message be shown only on submit button click event. Thanks Commented Jan 3, 2017 at 5:03

0

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.