因为AngularJS项目中经常结合Bootstrap的样式或组件开发更漂亮美观的页面,所以建议读者先对Bootstrap做个简单了解。
- Bootstrap 是最受欢迎的 HTML、CSS 和 JS 框架,用于开发响应式布局、移动设备优先的 WEB 项目。
- 需要下载的css样式表:bootstrap.min.css;js文件:angular.js → 从我的网盘分享获取
ng实现表单验证
- 导入css&js(路径根据自己的存放目录而定)
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<script src="js/angular.js"></script>
</head>
- 表单及验证
<body>
<div ng-cloak ng-app="myApp" style="margin-top: 60px;">
<form name="myForm" action="route.html" ng-controller="formController" class="container form-horizontal">
<h1 style="margin-left: 330px;">Please complete registration...</h1>
<p> </p>
<div class="form-group has-feedback" ng-class="{'has-error':myForm.username.$dirty && myForm.username.$invalid}">
<label class="col-sm-4 control-label">用户名</label>
<div class="col-sm-4">
<input type="text" autocomplete="off" name="username" ng-pattern="/^[a-zA-Z]{1}/" ng-required="true" required autofocus ng-minlength="2" ng-maxlength="7" ng-model="data.username" class="form-control" placeholder="start with an English letter">
<div ng-show="myForm.username.$dirty && myForm.username.$error.maxlength" class="alert alert-danger help-block">
用户名长度不能超过7位
</div>
<div ng-show="myForm.username.$dirty && myForm.username.$error.minlength" class="alert alert-danger help-block">
用户名长度不能少于2位
</div>
<div ng-show="myForm.username.$dirty && myForm.username.$error.pattern" class="alert alert-danger help-block">
用户名首位必须为英文字母
</div>
</div>
</div>
<div class="form-group" ng-class="{'has-error':myForm.age.$dirty && myForm.age.$invalid}">
<label class="col-sm-4 control-label">年龄</label>
<div class="col-sm-4">
<input type="number" autocomplete="off" name="age" min="1" max="99" ng-required="true" ng-model="data.age" class="form-control" placeholder="1~99">
<div ng-show="myForm.age.$dirty && myForm.age.$error.max" class="alert alert-danger help-block">
年龄不能超过99岁
</div>
<div ng-show="myForm.age.$dirty && myForm.age.$error.min" class="alert alert-danger help-block">
年龄不能小于1岁
</div>
</div>
</div>
<div class="form-group" ng-class="{'has-error':myForm.email.$dirty && myForm.email.$invalid}">
<label class="col-sm-4 control-label">邮箱</label>
<div class="col-sm-4">
<input type="email" autocomplete="off" name="email" ng-required="true" ng-minlength="5" ng-maxlength="30" ng-model="data.email" class="form-control" placeholder="123@example.com">
<div ng-show="myForm.email.$dirty && myForm.email.$error.maxlength" class="alert alert-danger help-block">
邮箱长度不能超过30位
</div>
<div ng-show="myForm.email.$dirty && myForm.email.$error.minlength" class="alert alert-danger help-block">
邮箱长度不能少于5位
</div>
<div ng-show="myForm.email.$dirty && myForm.email.$error.email" class="alert alert-danger help-block">
邮箱格式不正确
</div>
</div>
</div>
<div class="form-group" ng-class="{'has-error':myForm.password.$dirty && myForm.password.$invalid}">
<label class="col-sm-4 control-label">密 码</label>
<div class="col-sm-4">
<input type="password" autocomplete="off" name="password" ng-required="true" ng-minlength="6" ng-maxlength="10" ng-model="data.password" class="form-control" placeholder="password">
<div ng-show="myForm.password.$dirty && myForm.password.$error.maxlength" class="alert alert-danger help-block">
密码长度不能超过10位
</div>
<div ng-show="myForm.password.$dirty && myForm.password.$error.minlength" class="alert alert-danger help-block">
密码长度不能少于6位
</div>
</div>
</div>
<div class="form-group" ng-class="{'has-error':myForm.passwordConfirm.$dirty && myForm.passwordConfirm.$invalid}">
<label class="col-sm-4 control-label">确认密码</label>
<div class="col-sm-4">
<input type="password" autocomplete="off" name="passwordConfirm" ng-required="true" ng-model="data.passwordConfirm" class="form-control" placeholder="confirm password">
<div ng-show="myForm.password.$dirty && myForm.passwordConfirm.$dirty && data.password!==data.passwordConfirm" class="alert alert-danger help-block">
确认密码与密码不一致
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-4">
<button type="submit" class="btn btn-success col-sm-6" ng-click="confirm()" ng-disabled="myForm.$invalid||data.passwordConfirm!==data.password">注册</button>
<button type="reset" class="btn btn-info col-sm-6" ng-click="reset()">重置</button>
</div>
</div>
</form>
</div>
<script>
angular.module('myApp', [])
.controller('formController', ['$scope', function($scope) {
$scope.confirm = function() {
alert("恭喜你注册成功!")
}
$scope.reset = function() {
$scope.myForm.$setPristine();
}
}]);
</script>
</body>
-
ng-show=""
的内容表示验证格式符合则隐藏错误提示,否则显示` ; -
ng-disabled=""
表示注册按钮在表单验证成功前是禁用状态; -
ng-click="confirm()"
表示验证成功后弹窗提示; -
action="route.html
表示跳转页面 -
效果图
埋个伏笔:跳转后的route.html页面是这样的↓
想要实现图中的几个小功能,对ng路由感兴趣的小伙伴戳我的这篇文章 AngularJS路由实现-计算器、留言板、备忘录