AngularJS手动表单验证
程序员文章站
2023-09-06 09:34:11
所谓手动验证是通过angularjs表单的属性来验证,而成为angularjs表单必须满足两个条件:
1、给form元素加上novalidate="novalidate"...
所谓手动验证是通过angularjs表单的属性来验证,而成为angularjs表单必须满足两个条件:
1、给form元素加上novalidate="novalidate";
2、给form元素加上name="theform",
如下:
<!doctype html> <html lang="en" ng-app="myapp1"> <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.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 submitting</a> </div> </div> </nav> <div class="container main-content" ng-controller="myctrl1"> <!--novalidate让表单不要使用html验证--> <!--theform变成scope的一个字段--> <form ng-submit="onsubmit(theform.$valid)" novalidate="novalidate" name="theform"> <div class="form-group"> <label for="name">name</label> <input type="text" class="form-control" id="name" ng-model="formmodel.name"/> </div> <div class="form-group" ng-class="{ 'has-error': !theform.email.$valid && (!theform.$pristine || theform.$submitted), 'has-success': theform.email.$valid && (!theform.$pristine || theform.$submitted) }"> <label for="email">email</label> <input type="email" class="form-control" id="email" ng-model="formmodel.email" required="required" name="email"/> <p class="help-block" ng-show="theform.email.$error.required && (!theform.$pristine || theform.$submitted)">必填</p> <p class="help-block" ng-show="theform.email.$error.email && (!theform.$pristine || theform.$submitted)">email格式不正确</p> </div> <div class="form-group"> <label for="username">username</label> <input type="text" class="form-control" id="username" ng-model="formmodel.username"/> </div> <div class="form-group"> <label for="age">age</label> <input type="number" class="form-control" id="age" ng-model="formmodel.age"/> </div> <div class="form-group"> <label for="sex">sex</label> <select name="sex" id="sex" class="form-control" ng-model="formmodel.sex"> <option value="">please choose</option> <option value="male">mail</option> <option value="femail">femail</option> </select> </div> <div class="form-group"> <label for="password">password</label> <input type="text" class="form-control" id="password" ng-model="formmodel.password"/> </div> <div class="form-group"> <button class="btn btn-primary" type="submit">register</button> </div> <pre> {{theform | json}} </pre> </form> </div> <script src="../node_modules/angular/angular.min.js"></script> <script src="second.js"></script> </body> </html>
- ● 给form加上novalidate="novalidate"意味着表单将不再使用html5验证特性
- ● 给form加上name="theform"意味着表单的名称是theform。如何使用theform,比如我们验证表单是否被修改过theform.$submitted
- ● 通过ng-submit提交表单
- ● formmodel是$scope中的一个属性
- ● 对表单的email进行了手动验证,使用了angularjs表单的众多属性,比如theform.email.$valid,theform.$pristine,theform.$submitted, theform.email.$error.required,theform.email.$error.email
- ● 通过<pre>{{theform | json}}</pre>把angularjs表单的所有属性都打印出来
{ "$error": { "required": [ { "$validators": {}, "$asyncvalidators": {}, "$parsers": [], "$formatters": [ null ], "$viewchangelisteners": [], "$untouched": true, "$touched": false, "$pristine": true, "$dirty": false, "$valid": false, "$invalid": true, "$error": { "required": true }, "$name": "email", "$options": null } ] }, "$name": "theform", "$dirty": false, "$pristine": true, "$valid": false, "$invalid": true, "$submitted": false, "email": { "$validators": {}, "$asyncvalidators": {}, "$parsers": [], "$formatters": [ null ], "$viewchangelisteners": [], "$untouched": true, "$touched": false, "$pristine": true, "$dirty": false, "$valid": false, "$invalid": true, "$error": { "required": true }, "$name": "email", "$options": null }, "sex": { "$validators": {}, "$asyncvalidators": {}, "$parsers": [], "$formatters": [], "$viewchangelisteners": [], "$untouched": true, "$touched": false, "$pristine": true, "$dirty": false, "$valid": true, "$invalid": false, "$error": {}, "$name": "sex", "$options": null } }
以上,凡是有name属性的input都被显示在上面。
在second.js文件中定义了module,controller以及提交表单的方法。
var myapp1 = angular.module('myapp1',[]); myapp1.controller('myctrl1', function($scope, $http){ $scope.formmodel = {}; $scope.onsubmit = function(){ $http.post('someurl',$scope.formmodel) .success(function(data){ console.log(':)'); }) .error(function(data){ console.log(':('); }); console.log($scope.formmodel); }; });
以上的表单验证方式好处是可控性强,但相对繁琐。
以上就是本文的全部内容,希望对angularjs手动表单验证能够熟练操作。
上一篇: 预支