How to check the correctness of several fields of a form field in angular?

I have a form called myForm and I have many required fields.

<input type="text" name="form.user.name" ng-model="form.user.name" required/>
<input type="text" name="form.user.email" ng-model="form.user.email" required/>
<input type="text" name="form.user.phone" ng-model="form.user.phone" required/>

<input type="text" name="form.user.accountNumber" ng-model="form.bank.accountNumber" required/>
<input type="text" name="form.user.accountName" ng-model="form.bank.accountName" required/>

Now I just want to check the accuracy of only the information form.user. How can i do this?

+4
source share
3 answers

Follow this link first
https://docs.angularjs.org/guide/forms

Custom Validation Example

<form name="form" class="css-form" novalidate>
  <div>
    <label>
    Size (integer 0 - 10):
    <input type="number" ng-model="size" name="size"
           min="0" max="10" integer />{{size}}</label><br />
    <span ng-show="form.size.$error.integer">The value is not a valid integer!</span>
    <span ng-show="form.size.$error.min || form.size.$error.max">
      The value must be in range 0 to 10!</span>
  </div>

</form>

In the line below ng-show, formis the name of the form, sizeis the name of the input field, $erroris the default object for storing failed validators. integeris a directive for validating an input field.

<span ng-show="form.size.$error.integer">The value is not a valid integer!</span>

script

var app = angular.module('form-example1', []);

var INTEGER_REGEXP = /^-?\d+$/;
app.directive('integer', function() {
  return {
    require: 'ngModel',
    link: function(scope, elm, attrs, ctrl) {
      ctrl.$validators.integer = function(modelValue, viewValue) {
        if (ctrl.$isEmpty(modelValue)) {
          // consider empty models to be valid
          return true;
        }

        if (INTEGER_REGEXP.test(viewValue)) {
          // it is valid
          return true;
        }

        // it is invalid
        return false;
      };
    }
  };
});
+2

..

<form> ngApp, AngularJS ( , form, ).

, , $scope.yourformname.$valid .

name form name .

HTML

<form name="someForm" action="/">
    <input name="username" required />
    <input name="password" type="password" required />
</form>

JS

$scope.someForm.username.$valid
// > false
$scope.someForm.password.$error
// > { required: true }

Guide .

+1

angular https://docs.angularjs.org/guide/forms . .

   <input type="text" name="form.user.name" ng-model="form.user.name" required/>
<input type="text" name="form.user.email" ng-model="form.user.email" required/>
<input type="text" name="form.user.phone" ng-model="form.user.phone" required/> // same name all three name="form.user.phone"

<input type="text" name="form.user.phone" ng-model="form.bank.accountNumber" required/> // same name all three name="form.user.phone"
<input type="text" name="form.user.phone" ng-model="form.bank.accountName" required/> // same name all three name="form.user.phone"
0

Source: https://habr.com/ru/post/1665189/


All Articles