5

How to check all fields in the submit form ng-submit="submit(field)" is empty in controller.js? They consists of few textboxes and one select field.

I would want an alert box to occur if all fields are empty and select option is not selected, instead of individual validation.

4 Answers 4

5

While it's not exactly what you're asking about, may be $pristine is what you want? It's a flag on the form indicating whether the form has ever been edited. If someone typed and then cleared out a field, $pristine would still be false, however.

<form name="myform" ng-submit="doSubmit()" ng-controller="FormController">
    <input ng-model="firstName" name="firstName" />

</form>

Then in your controller

.controller('FormController', function($scope){
    $scope.doSubmit = function(){
        if($scope.myform.$pristine){}
    }
})

Alternatively, you can set all your fields to required="true" and use the $valid flag on the form in the same way as described above.

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

2 Comments

$scope.myform.$pristine is undefined. I need to do anything else? No?
Did you name the form and the input element? Is the controller your accessing the $scope in in the right spot to have the form in its $scope? jsfiddle.net/1774oawr
0

You can use this:

ng-show="!yourfield.length"

Something like this:

<form name="yourform">
  <input name="yourfield" ng-model="somefield" ng-minlength="10" required>
  <span ng-show="!yourfield.myfield.$error.required">Something</span>
</form> 

1 Comment

i would one a one time validation after submitting. If all fields are empty, an alert box will popup, instead of individual validation
0

You should look for Form Validation . Take a look at this tutorial.

<input name="name" class="form-control"
    ng-model="user.name" placeholder="Name" required>
  <div class="alert alert-danger"
    ng-show="userForm.name.$error.required">
    Please enter the name.
  </div>

Comments

0

The following will show an alert if all fields are empty

angular.module('app', [])
.controller('FormController', function($scope) {
  $scope.doSubmit = function(){
    if (formIsEmpty($scope.myform)){
      alert('You forgot to enter something before submitting');
    }
  };
  
  function formIsEmpty(form) {
    for (var prop in form) {
      if (!form.hasOwnProperty(prop)) { continue; }
      if (prop[0] === '$') { continue; }
      if ($scope[prop]) { return false; }
    }
    return true;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <form name="myform" ng-submit="doSubmit()" ng-controller="FormController">
    <input ng-model="firstName" name="firstName" />
    <input ng-model="lastName" name="lastName" />
    <textarea ng-model="description" name="description"></textarea>
    <button type="submit">Submit</button>
  </form>
</div>

1 Comment

It works fine. But id doesn't work when we use form object. For eg. <input ng-model="user.firstName" name="firstName" /> OR <input ng-model="user.firstName" name="user.firstName" /> Can you please tell me how to do?

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.