如何检查angularjs中的所有字段都是空的

时间:2022-11-24 19:43:48

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.

如何检查提交表单中的所有字段ng-submit =“submit(field)”在controller.js中是空的?它们由少量文本框和一个选择字段组成。

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

如果所有字段都为空并且未选择选项,而不是单独验证,我希望发生警告框。

Thanks

谢谢

4 个解决方案

#1


4  

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.

虽然它不完全是你所询问的,但可能是$ pristine是你想要的?它是表单上的一个标志,表示表单是否曾被编辑过。如果有人输入然后清除了一个字段,那么$ pristine仍然是假的。

<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.

或者,您可以将所有字段设置为required =“true”,并以与上述相同的方式在表单上使用$ valid标志。

#2


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> 

#3


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>

#4


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


4  

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.

虽然它不完全是你所询问的,但可能是$ pristine是你想要的?它是表单上的一个标志,表示表单是否曾被编辑过。如果有人输入然后清除了一个字段,那么$ pristine仍然是假的。

<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.

或者,您可以将所有字段设置为required =“true”,并以与上述相同的方式在表单上使用$ valid标志。

#2


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> 

#3


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>

#4


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>