AngularJS automatic form validation

  • 2020-12-13 18:50:41
  • OfStack

Another form verification method of AngularJS is automatic verification, that is, through directive. In addition to directive, which comes with AngularJS, angular-ES6en-ES7en, the third side of module, is also needed.

About angular auto - validate:

Installation: npm i ES18en-ES19en-ES20en Reference: < script src="../node_modules/angular-auto-validate/dist/jcs-auto-validate.min.js" > < /script > module dependent: var myApp = angular.module("app", [" ES33en-ES34en "]);

To localize the error message, the third party, ES36en-ES37en, is also needed:

Installation: npm install ES42en-ES43en --save module dependent: var myApp = angular.module("app", ["localize"]); Reference:

<script src="../node_modules/angular-sanitize/angular-sanitize.min.js"></script>
<script src="../node_modules/angular-localize/angular-localize.min.js"></script>



In addition, when the form submission button is clicked, the button needs to be disabled and a waiting effect needs to be displayed. The third party, ES54en-ES55en, needs to be used:

Installation: bower install ES60en-ES61en --save module dependent: var myApp = ES66en.module ("app", [" ES69en-ES70en "]); Reference:

<link rel="stylesheet" href="../bower_components/ladda/dist/ladda-themeless.min.css"/>

<script src="../bower_components/ladda/dist/spin.min.js"></script>
<script src="../bower_components/ladda/dist/ladda.min.js"></script>
<script src="../bower_components/angular-ladda/dist/angular-ladda.min.js"></script>

The page is as follows:


<html>
<head>
 <meta charset="gb2312">
 <title></title>
 <link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css"/>
 <link rel="stylesheet" href="../bower_components/ladda/dist/ladda-themeless.min.css"/>
 <link rel="stylesheet" href="../css/main.css"/>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
 <div class="container">
 <div class="navbar-header">
  <a href="/" class="navbar-brand">Form Validating Auto</a>
 </div>
 </div>
</nav>

<div class="container main-content" ng-controller="myCtrl1">
 <!--novalidate Don't use forms html validation -->
 <!--theForm become scope the 1 A field -->
 <form ng-submit="onSubmit()" novalidate="novalidate">
 <div class="form-group">
  <label for="name" class="control-label">Name</label>
  <input type="text" class="form-control" id="name" ng-model="formModel.name" required="required"/>
 </div>

 <div class="form-group">
  <label for="email" class="control-label">Email</label>
  <input type="email" class="form-control" id="email" ng-model="formModel.email" required="required"/>

 </div>

 <div class="form-group">
  <label for="username" class="control-label">Username</label>
  <input type="text"
    class="form-control"
    id="username"
    ng-model="formModel.username"
    required="required"
    ng-pattern="/^[A-Za-z0-9_]{1,32}$/"
    ng-minlength="7"
    ng-pattern-err-type="badUsername"
  />
 </div>

 <div class="form-group">
  <label for="age" class="control-label">Age</label>
  <input type="number"
    class="form-control"
    id="age"
    ng-model="formModel.age"
    required="required"
    min="18"
    max="65"
    ng-min-err-type="tooYoung"
    ng-max-err-type="tooOld"
  />
 </div>

 <div class="form-group">
  <label for="sex" class="control-label">Sex</label>
  <select name="sex" id="sex" class="form-control" ng-model="formModel.sex" required="required">
  <option value="">Please choose</option>
  <option value="male">Mail</option>
  <option value="femail">Femail</option>
  </select>
 </div>

 <div class="form-group">
  <label for="password" class="control-label">Password</label>
  <input type="text" class="form-control" id="password" ng-model="formModel.password" required="required" ng-minlength="6"/>
 </div>

 <div class="form-group">
  <!--<button class="btn btn-primary" ng-click="onSubmit()">Register</button>-->
  <button class="btn btn-primary"
    ladda = "submitting"
    data-style="expand-right"
    type="submit">
  <span ng-show="submitting"> Is the registered ...</span>
  <span ng-show="!submitting"> registered </span>
  </button>
 </div>

  <pre>
  {{formModel | json}}
  </pre>
 </form>
</div>
<script src="../node_modules/angular/angular.min.js"></script>

<script src="form_validation_auto.js"></script>
<script src="../node_modules/angular-auto-validate/dist/jcs-auto-validate.min.js"></script>
<script src="../node_modules/angular-sanitize/angular-sanitize.min.js"></script>
<script src="../node_modules/angular-localize/angular-localize.min.js"></script>

<script src="../bower_components/ladda/dist/spin.min.js"></script>
<script src="../bower_components/ladda/dist/ladda.min.js"></script>
<script src="../bower_components/angular-ladda/dist/angular-ladda.min.js"></script>

</body>
</html>

First look at the submit button:


<div class="form-group">
 <!--<button class="btn btn-primary" ng-click="onSubmit()">Register</button>-->
 <button class="btn btn-primary"
   ladda = "submitting"
   data-style="expand-right"
   type="submit">
 <span ng-show="submitting"> Is the registered ...</span>
 <span ng-show="!submitting"> registered </span>
 </button>
</div>
 

ladda attribute value is bool value, true shows dynamic wait effect, false does not show dynamic wait effect, submitting here is one of the attributes of scope data-style =" expand-ES90en "indicates that the dynamic wait effect is displayed on the right side of the button

Take the Age field in the form:


<div class="form-group">
 <label for="age" class="control-label">Age</label>
 <input type="number"
   class="form-control"
   id="age"
   ng-model="formModel.age"
   required="required"
   min="18"
   max="65"
   ng-min-err-type="tooYoung"
   ng-max-err-type="tooOld"
 />
</div>

Among them,min and max are directive of AgularJS, and ES100en-ES101en-ES102en-ES103en is directive of ES104en-ES105en-ES106en. The convention followed here is the directive name for ng-ES109en form validation -ES111en-ES112en. What do tooYoung and tooOld do and where do they use them?

It is used at the module level and is defined in the form_ES119en_auto.js file.


var myApp1 = angular.module('myApp1',['jcs-autoValidate','localize','angular-ladda']);

myApp1.run(function(defaultErrorMessageResolver){
 defaultErrorMessageResolver.getErrorMessages().then(function(errorMessages){
  errorMessages['tooYoung'] = ' Must be younger than {0}';
  errorMessages['tooOld'] = ' Age must not be greater than {0}';
  errorMessages['badUsername'] = ' User names can only contain numbers, letters, or underscores ';
 });
});

myApp1.controller('myCtrl1', function($scope, $http){
 $scope.formModel = {};
 $scope.submitting = false;

 $scope.onSubmit = function(){

  $scope.submitting = true;
  console.log(' Has been submitted ');
  console.log($scope.formModel);

  $http.post('url',$scope.formModel)
   .success(function(data){
    console.log(':)');
    $scope.submitting = false;
   })
   .error(function(data){
    console.log(':(');
    $scope.submitting = false;
   });
 };
});

That's it for this article, so you want to be proficient with AngularJS manual form validation.


Related articles: