AngularJSb表单校验示例
2015-10-27 13:19
696 查看
<!DOCTYPE HTML> <html ng-app="MyApp"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <script type="text/javascript" src="js/angular/angular.min.js"></script> <link rel="stylesheet" type="text/css" href="js/bootstrap/css/bootstrap.min.css"/> <link rel="stylesheet" type="text/css" href="js/bootstrap/css/bootstrap-theme.min.css"/> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/bootstrap/js/bootstrap.min.js"></script> <style type="text/css"> .error{ color:red;font-size:12px; } </style> <script type="text/javascript"> var myApp = angular.module("MyApp",[]); myApp.config(function($interpolateProvider){ $interpolateProvider.startSymbol("[["); $interpolateProvider.endSymbol("]]"); }); myApp.controller("MyController",["$scope",function($scope){ $scope.showLoginDialog = function(){ jQuery("#loginModal").modal("show"); }; $scope.closeLoginDialog = function(){ jQuery("#loginModal").modal("hide"); }; $scope.sumbitLogin = function(){ if($scope.loginForm.$invalid){ $scope.loginForm.submitted = true; }else{ alert("点击提交表单"); } }; }]); </script> </head> <body ng-controller="MyController"> <input type="button" class="btn btn-primary" value="登陆" ng-click="showLoginDialog()"/> <!-- login modal --> <div id="loginModal" class="modal fade"> <div class="modal-dialog" style="width:400px;"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title">登陆</h4> </div> <form name="loginForm" class="form-horizontal" method="post" ng-submit="sumbitLogin()"> <div class="modal-body"> <div class="form-group form-group-sm"> <label for="username" class="col-sm-3 control-label">用户名</label> <div class="col-sm-9"> <div class="error" ng-show="loginForm.username.$dirty&&loginForm.username.$invalid&&loginForm.submitted"> 用户名为3~10位长度的中文或英文字符串 </div> <input name="username" ng-model="username" type="text" class="form-control" id="username" ng-minlength="3" ng-maxlength="10" ng-pattern="/^[A-Za-z0-9]+$/" required/> </div> </div> <div class="form-group form-group-sm"> <label for="password" class="col-sm-3 control-label">密码</label> <div class="col-sm-9"> <div class="error" ng-show="loginForm.password.$dirty&&loginForm.password.$invalid&&loginForm.submitted"> 密码长度不能超过20 </div> <input name="password" ng-maxlength="20" ng-model="password" type="password" class="form-control" id="password" required/> </div> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-primary">登陆</button> <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="closeLoginDialog()">关闭</button> </div> </form> </div> </div> </div> </body> </html>
效果如下:
相关文章推荐
- AngularJS 指令
- AngularJS 表达式
- AngularJS 简介
- 有关AngularJS请求Web API资源的思路
- angular 获取filter
- AngularJS开发人员最常犯的10个错误
- angularjs compile vs link
- Angularjs-Forms(表单)
- angular中通过事件获取ng-repeat中每个子项的属性
- AngularJS拖动窗口滑块以加载更多
- Angularjs-filter(过滤器)
- angular.bind使用心得
- [Angular 2] Passing data to components with @Input
- [Angular 2] Adding a data model
- [Angular 2] Using ng-model for two-way binding
- [Angular 2] ngFor
- ng-cloak解决angularJS中的闪烁
- angular.bind使用心得
- angularjs中常用的工具方法
- Angular引入百分比过滤器