13

Given a simple html form like this:

<form name="myForm" action="#sent" method="post" ng-app>
   <input name="userPreference1" type="text" ng-model="shipment.userPreference" />
   <input name="userPreference1" type="text" ng-model="shipment.userPreference" />
   <input name="userPreference1" type="text" ng-model="shipment.userPreference" />
... submit input and all the other code...
</form>

I need your help to know how to check on validation time, if at least one of the inputs is empty. The desired validation is the following. The user must complete at least one a preference.

Using jQuery this:

if ( $("input").val() == "" ) {

Works ok, but would like to figure out how to do the same thing using angular.

Thanks so much in advance,

Guillermo

3 Answers 3

29

So the idea is to disable the submit button if all inputs are blank. You can do like this

<form name="myForm" action="#sent" method="post" ng-app>
   <input name="userPreference1" type="text" ng-model="shipment.userPreference1" />
   <input name="userPreference1" type="text" ng-model="shipment.userPreference2" />
   <input name="userPreference1" type="text" ng-model="shipment.userPreference3" />

   <button type="submit" ng-disabled="!(!!shipment.userPreference1 || !!shipment.userPreference2  || !!shipment.userPreference3)">Submit</button>
</form>

!!str is to force to convert str to a boolean value. And both !!null and !!"" are evaluated to be false.

Demo

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

9 Comments

sza, thanks for your answer! The user won't be required to enter all the fields, just one, so because of that I need to check when validating if at least one input was populated.
@Guillermo Then they should have different model.
@Guillermo Checkout ng-repeat. Keep in mind, AngularJS is data/model driven, if you want to manipulate the dom in normal case, just think in how to design the data structure. And angularjs will magically change the dom for you.
There's a better way of doing this. On each of your inputs, add ng-required. That will cause your form controller to run validation, then your <button> statement simply becomes ng-disabled="frmTicket.$invalid"
You don't need the !!string - just use !(shipment.userPreference1 || shipment.userPreference2 || shipment.userPreference3)
|
4

You can set the "required" in the input elements and style / code how you want to handle with $valid of a form. Check out http://dailyjs.com/2013/06/06/angularjs-7/

1 Comment

Mouli, thanks for your answer, I know it's weird, but in the original state, the user will see three inputs, and will be required to fill at least one, but not all of them. So that's why I think that adding required won't fit for me. I need some equivalent of $("input").val() == "" applied to angularjs. Thanks!!
4

My solution was the following:

        $scope.requiredInputsGroup = function () {
            var isRequired = true;
            if (!$scope.shipment) {
                return true;
            }
            angular.forEach(["userPreference1", "userPreference2", "userPreference3"], function (input) {
                if ($scope.shipment[input]) {
                    isRequired = false;
                    return false;
                }
            });

            return isRequired;
        };

You apply that method to a data-ng-required in each of the inputs...

<form name="myForm" action="#sent" method="post" ng-app>
   <input name="userPreference1" type="text" ng-model="shipment.userPreference1" ng-required="requiredInputsGroup()" />
   <input name="userPreference2" type="text" ng-model="shipment.userPreference2" ng-required="requiredInputsGroup()" />
   <input name="userPreference3" type="text" ng-model="shipment.userPreference3" ng-required="requiredInputsGroup()" />

   <button type="submit" ng-disabled="myForm.$invalid">Submit</button>
</form>

And the last bit I applied was to make the button disabled with a simple myForm.$invalid

1 Comment

Great solution! I altered it so you can pass in the model you are testing against instead of passing an array into the forEach. The only problem was testing against checkboxes which return an 'N' when not checked, but that was really easy to check for in the if. Thanks!

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.