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 ...
随机推荐
- [转]数据库性能优化之SQL语句优化1
一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用系统提交实际应用后,随着数据库中数据的增加,系统 ...
- Summary Checklist for Run-Time Kubernetes Security
Here is a convenient checklist summary of the security protections to review for securing Kubernetes ...
- Spatial Sound Research
Spatial Sound Research What are our goals? The basic goal of our research is to develop cost-effecti ...
- java.util.concurrent介绍【转】
java.util.concurrent介绍 java.util.concurrent 包含许多线程安全.测试良好.高性能的并发构建块.不客气地说,创建 java.util.concurrent ...
- js调用winform程序(带参数)
我们会发现,我们点击迅雷下载的时候 网页可以调用应用程序,而且连接会传入迅雷,这个是怎么做到的呢? 原理: 先注册表中添加软件的具体信息,然后通过 href 可以直接调用 1.写入注册表信息,注册, ...
- Spark学习笔记——安装和WordCount
1.去清华的镜像站点下载文件spark-2.1.0-bin-without-hadoop.tgz,不要下spark-2.1.0-bin-hadoop2.7.tgz 2.把文件解压到/usr/local ...
- openwrt 域名ping不通 修改dns
问题 wget: bad address 'downloads.openwrt.org' wget: bad address 'downloads.openwrt.org' wget: bad add ...
- ABBYY OCR技术教电脑阅读缅甸语(上)
缅甸联邦共和国,原名缅甸,是东南亚的一个国家,从1962年到2010年,缅甸一直被政变后上台的军政府统治,直至最近5年它才对外界开放,与其他国家建立了贸易与文化联系. 缅甸语由很多方言组成,但所有方言 ...
- IOC容器特性注入第二篇:初始引擎查找对应的IOC容器
上篇文章介绍了如何利用反射类查找网站bin文件夹下面所有DLL的程序集类,这篇文章将介绍如何初始化一个IOC容器引擎. 目前IOC容器有很多,如Ninject,Autofac等,每个容器的驱动都不一样 ...
- AnalyticDB - 分析型数据库
https://yq.aliyun.com/teams/31?spm=5176.7937365.1120968.ee1.78505692UL9DhG 分析型数据库(AnalyticDB)是一种高并发低 ...