0

Simple and easy.

I got two radio buttons.

<input type="radio" name="gender" ng-model="male">
<input type="radio" name="gender" ng-model="female">

How do i validate in AngularJS that at least one is chosen? And how can i type something like

$scope.myForm.gender.$invalid

Any ideas?

2

2 Answers 2

1

Without a form, you can fix this by check the value of the models in your controller, returning an error if both are false. You could also go ahead and set one true by default.

But to answer your question, you can do something similar to $scope.myForm.gender.$invalid all you have to do is wrap your input tags in a form with the name myForm. So, it would like:

<form name="myForm">
   <input type="radio" name="gender" ng-model="male">
   <input type="radio" name="gender" ng-model="female">
</form>

Then, $scope.myForm would be able to give you certain properties, like $isPristine and properties for each input field.

Either of these ways will work though, so I help that helps!

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

Comments

0

use required :

   <input type="radio" name="gender" ng-model="male" required>
   <input type="radio" name="gender" ng-model="female" required>

DEMO

same Question asked here : Validate Radio Button AngularJS

1 Comment

Why for example is this working perfectly but it doesn’t when the type=‘radio’ ??? <input type='checkbox' ng-init='checkStatus=false' ng-model='checkStatus' ng-click='doIfChecked(checkStatus)'>

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.