AngularJS Automatic Form Verification and AngularJS Form Verification

Source: Internet
Author: User

AngularJS Automatic Form Verification and AngularJS Form Verification

Another Form Verification Method of AngularJS is automatic verification, which is achieved through directive. In addition to the directive provided by AngularJS, the angular-auto-validate third-party module is also required.

About angular-auto-validate:

  • Installation: npm I angular-auto-validate
  • Reference: <script src = "../node_modules/angular-auto-validate/dist/jcs-auto-validate.min.js"> </script>
  • Module dependency: var myApp = angular. module ("app", ["jcs-autoValidate"]);

To localize error information, you also need the angular-localize third-party module:

  • Install: npm install angular-localize -- save
  • Module dependency: 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 you click the submit form button, you need to disable the button and display a waiting effect. You need to use the angular-ladda third-party module:

  • Install: bower install angular-ladda -- save
  • Module dependency: var myApp = angular. module ("app", ["angular-ladda"]);
  • 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> 

Click the submit button first:

<Div class = "form-group"> <! -- <Button class = "btn-primary" ng-click = "onSubmit () "> Register </button> --> <button class =" btn-primary "ladda =" submitting "data-style =" expand-right "type =" submit "> <span ng-show = "submitting"> registering... </span> <span ng-show = "! Submitting "> Registration </span> </button> </div>
  • The property value of ladda is a Boolean value. true indicates that the dynamic waiting effect is displayed. false indicates that the dynamic waiting effect is not displayed. submitting is an attribute in scope.
  • Data-style = "expand-right" indicates that the dynamic waiting effect is displayed on the right of the button.

Take the Age field in the form as an example:

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

Min and max are the directive of AgularJS, while ng-min-err-type is the directive of angular-auto-validate. The Convention follows here is the directive name-err-type for ng-AngularJS form verification. What is the role of tooYoung and tooOld and where is it used?

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

Var myApp1 = angular. module ('myapp1', ['jcs-autovalidate', 'localize', 'angular-ladda']); myApp1.run (function (defaultErrorMessageResolver) {defaultErrorMessageResolver. getErrorMessages (). then (function (errorMessages) {errorMessages ['tooyoung '] = 'Age must be less than {0}'; errorMessages ['tooold'] = 'Age cannot be greater than {0 }'; errorMessages ['badusername'] = 'user name 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 ('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 ;});};});

The above is all the content of this article. We hope to be proficient in AngularJS manual form verification.

Articles you may be interested in:
  • AngularJS implements Form Verification
  • Detailed explanation of Form Verification programming in AngularJS
  • Details about how AngularJS implements Form Verification
  • AngularJs implements ng1.3 + Form Verification
  • AngularJS uses ngMessages for Form Verification
  • AngularJS uses angular-formly for Form Verification
  • AngularJS manual Form Verification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.