AngularJS自动表单验证

AngularJS的另外一种表单验证方式是自动验证,即通过directive来实现,除了AngularJS自带的directive,还需要用到angular-auto-validate这个第三方module。

有关angular-auto-validate:

安装:npm i angular-auto-validate

引用:<script src="https://www.jb51.net/node_modules/angular-auto-validate/dist/jcs-auto-validate.min.js"></script>

module依赖:var myApp = angular.module("app", ["jcs-autoValidate"]);

为了实现错误信息本地化,还需要angular-localize这个第三方module:

安装:npm install angular-localize --save

module依赖:var myApp = angular.module("app", ["localize"]);

引用:

<script src="https://www.jb51.net/node_modules/angular-sanitize/angular-sanitize.min.js"></script> <script src="https://www.jb51.net/node_modules/angular-localize/angular-localize.min.js"></script>

此外,当点击提交表单按钮,需要禁用按钮并显示一种等待效果,需要用到angular-ladda这个第三方module:

安装:bower install angular-ladda --save

module依赖:var myApp = angular.module("app", ["angular-ladda"]);

引用:

<link href="https://www.jb51.net/bower_components/ladda/dist/ladda-themeless.min.css"/> <script src="https://www.jb51.net/bower_components/ladda/dist/spin.min.js"></script> <script src="https://www.jb51.net/bower_components/ladda/dist/ladda.min.js"></script> <script src="https://www.jb51.net/bower_components/angular-ladda/dist/angular-ladda.min.js"></script>

页面如下:

<html> <head> <meta charset="gb2312"> <title></title> <link href="https://www.jb51.net/node_modules/bootstrap/dist/css/bootstrap.min.css"/> <link href="https://www.jb51.net/bower_components/ladda/dist/ladda-themeless.min.css"/> <link href="https://www.jb51.net/css/main.css"/> </head> <body> <nav> <div> <div> <a href="https://www.jb51.net/">Form Validating Auto</a> </div> </div> </nav> <div ng-controller="myCtrl1"> <!--novalidate让表单不要使用html验证--> <!--theForm变成scope的一个字段--> <form ng-submit="onSubmit()" novalidate="novalidate"> <div> <label for="name">Name</label> <input type="text" ng-model="formModel.name" required="required"/> </div> <div> <label for="email">Email</label> <input type="email" ng-model="formModel.email" required="required"/> </div> <div> <label for="username">Username</label> <input type="text" ng-model="formModel.username" required="required" ng-pattern="/^[A-Za-z0-9_]{1,32}$/" ng-minlength="7" ng-pattern-err-type="badUsername" /> </div> <div> <label for="age">Age</label> <input type="number" ng-model="formModel.age" required="required" min="18" max="65" ng-min-err-type="tooYoung" ng-max-err-type="tooOld" /> </div> <div> <label for="sex">Sex</label> <select ng-model="formModel.sex" required="required"> <option value="">Please choose</option> <option value="male">Mail</option> <option value="femail">Femail</option> </select> </div> <div> <label for="password">Password</label> <input type="text" ng-model="formModel.password" required="required" ng-minlength="6"/> </div> <div> <!--<button ng-click="onSubmit()">Register</button>--> <button ladda = "submitting" data-style="expand-right" type="submit"> <span ng-show="submitting">正在注册...</span> <span ng-show="!submitting">注册</span> </button> </div> <pre> {{formModel | json}} </pre> </form> </div> <script src="https://www.jb51.net/node_modules/angular/angular.min.js"></script> <script src="https://www.jb51.net/form_validation_auto.js"></script> <script src="https://www.jb51.net/node_modules/angular-auto-validate/dist/jcs-auto-validate.min.js"></script> <script src="https://www.jb51.net/node_modules/angular-sanitize/angular-sanitize.min.js"></script> <script src="https://www.jb51.net/node_modules/angular-localize/angular-localize.min.js"></script> <script src="https://www.jb51.net/bower_components/ladda/dist/spin.min.js"></script> <script src="https://www.jb51.net/bower_components/ladda/dist/ladda.min.js"></script> <script src="https://www.jb51.net/bower_components/angular-ladda/dist/angular-ladda.min.js"></script> </body> </html>

先看提交按钮: 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwwjs.html