angular validation 使用总结
我由于制作登陆界面,用到了angular-validation,结合ng-cookies,实现记住账户密码的功能。文档是https://github.com/hueitan/angular-validation/blob/master/API.md
该文档挺详细的介绍了angular-validation的使用方式。我因为英文不好,理解的不是很仔细。但是总结了以下的使用经验。
1 首先在模块中注入validation模块
var app = angular.module("zdwq",["ui.router","ui.bootstrap","ngCookies","validation"]);
2 config.js文件中配置config ,设置验证规则的表达式以及文字
app.config(['"$validationProvider",function($validationProvider){
var expression = {
username:/^1[3|4|5|7|8]\d{9}$/,
password: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,18}$/,
required:function(value){
return !!value
}
};
var defaultMsg = {
username:{
success:"",
error:"必须是手机号"
},
password:{
success:"",
error:"长度不能小于8位,不能大于18位"
}
};
$validationProvider.setExpression(expression).setDefaultMsg(defaultMsg);
3 html结构中使用验证
<form name="loginForm">
<p class="pr"><input name="username_account" validator="username" type="text" class="login-name" placeholder="请输入账号" ng-model="user.username" valid-method="blur,submit"></p>
<p class="pr"><input name="password_account" validator="password" type="password" class="login-pwd" placeholder="请输入密码" ng-model="user.password" valid-method="blur,submit"></p>
<p><label class="remember-label"><i class="db vm login-checkbox" ng-class="user.remembered?'checked':''" alt=""></i><input type="checkbox" hidden class="login-remember" ng-model="user.remembered">记住账号</label></p>
<p><button class="login-btn" ng-click="submit(loginForm)" validator-submit="loginForm">登录</button></p>
</form>
注意 (1) form指定名字,方便ng-click调用函数作为参数使用$validation.validate(form).success(function(){}).error(function(){});
$scope.submit = function(form){
$validation.validate(form).success(function(){
$http.get("data/login.json").then(function(){
if($scope.user.remembered){
$cookies.put("zdwq_username",$scope.user.username);
$cookies.put("zdwq_password",$scope.user.password);
}
else{
if($cookies.get("zdwq_username")) $cookies.remove("zdwq_username");
if($cookies.get("zdwq_password")) $cookies.remove("zdwq_password");
}
$window.sessionStorage.setItem("haslogined",true) // 不关闭页面的情况下,刷新页面状态不变
alert("登录成功");
$rootScope.isUserAuth = true;
},function(){
alert("登录失败")
})
}).error(function(){
// alert("请输入正确的账号或密码")
})
}
(2) input 中指定validator,表示使用哪个验证规则,另外input中可指定valid-method 表示采用哪种方式对input使用规则,有watch,blur,submit,submit-only方式,默认是watch。需要在点击登录按钮验证的时候,需要指定submit,submit-only 中的一种方式,但是需要为input指定name(没有限制名字的要求) .为了取得好看的效果,我制作的页面中使用了blur与submit的组合方式。
angular validation 使用总结的更多相关文章
- [Angular2 Form] Style Validation in Angular 2 Forms
Inputs using Angular 2’s ngModel automatically apply style classes of .ng-validand .ng-invalid each ...
- [Angular2 Form] Display Validation and Error Messaging in Angular 2
Angular 2’s ngModel provides error objects for each of the built-in input validators. You can access ...
- [Angular 2] Validation
Define a filed should has validation: export class DemoFormSku { myForm: ControlGroup; sku: Abstract ...
- [Angular2 Form] Create Radio Buttons for Angular 2 Forms
Using Radio Buttons in Angular 2 requires a basic understanding of forms as well as how their labels ...
- angular : ngModel 内部流程
angular 1.5 beta link NgModelController provides API for the ngModel directive. The controller conta ...
- 创建自定义的 Angular Schematics
本文对 Angular Schematics 进行了介绍,并创建了一个用于创建自定义 Component 的 Schematics ,然后在 Angular 项目中以它为模板演练了通过 Schemat ...
- [转]Angular: Hide Navbar Menu from Login page
本文转自:https://loiane.com/2017/08/angular-hide-navbar-login-page/ In this article we will learn two ap ...
- Angular 2 to Angular 4 with Angular Material UI Components
Download Source - 955.2 KB Content Part 1: Angular2 Setup in Visual Studio 2017, Basic CRUD applicat ...
- User Authentication with Angular and ASP.NET Core
User authentication is a fundamental part of any meaningful application. Unfortunately, implementing ...
随机推荐
- 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx EF 6 Code-Firs ...
- Ubunt 使用Virtualbox虚拟机NAT无法上网解决办法
我的Ubuntu安装了一个Centos虚拟机,为了SSH和上外网的方便,使用了NAT+host Only方式,实现内网+外网,但是安装好的Centos不能连接外网,很是无语,只能Google了-- 解 ...
- 远程桌面中Tab键不能补全的解决办法
我们曾在之前的一篇文章中介绍了windows远程连接ubuntu的方法,在成功登陆远程桌面环境之后,发现在终端中Tab键不能自动补齐(但是Ctrl +Tab 可以用,但是需要按下组合键才能补全的话,时 ...
- oracle11g重新安装oem
1.重新设置sys sysman DBSNMP密码 alter user dbsnmp identified by **: 2.select 'drop public synonym '|| syno ...
- java 实现websocket
最近了解了下websocket和socket这个东西,说不得不来说下为何要使用 WebSocket ,和为何不用http. 为何需要WebSocket ? HTTP 协议是一种无状态的.无连接的.单向 ...
- 【iCore4 双核心板_FPGA】例程十五:基于单口RAM的ARM+FPGA数据存取实验
实验现象: 写RAM命令格式:write:地址(0-255),数据(0-65535)\cr\lf 读RAM命令格式:read:地址(0-255)\cr\lf 核心代码: int main(void) ...
- c++ clr编译dll在c#调用时出现“试图加载不正确的格式”“找不到dll”错误的解决
用depends发现缺了一堆API-MS-WIN什么的dll,网上查找是因为少了VC++2010,VC++2015等一系列,装好后仍然不行,原来这种错误并不是该原因导致的,也并不缺少那些dll(dep ...
- 使用InstallAnywhere7.1制作Java exe程序安装包
[转[使用InstallAnywhere7.1制作Java exe程序安装包 使用InstallAnywhere7.1制作Java exe程序安装包 对于已经完成的Java应用程序开发项目,从商业化角 ...
- java指纹识别+谷歌图片识别技术_源代码
主类: import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; public c ...
- 年关将至业内警示P2P跑路风险
年关将近,P2P网贷行业的问题平台亦不断增多,“跑路潮”会否再现,业内人士讨论热烈. “从历年数据统计来看,问题平台接近线性向上的增长趋势,即时间越往后,问题平台占比就越高,而每逢年关,问题平台占比都 ...