angularjs学习第四天笔记(第一篇:简单的表单验证)
您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点评与赐教。谢谢!
第四天,简单的表单验证,今天主要学习了angularjs中的表单验证的一些基本概念及其简单应用
第一、表单验证的简单理解
表单验证是angularjs中比较重要的一个核心功能
表单验证可以结合html5的验证特殊使用,当然也可以禁用浏览器对表单的默认验证,添加属性【novalidate】即可
表单验证毕竟只是前端js验证,在后端代码中一定需要对其数据的合法性做再次验证
angularjs提供了一些常见的系统验证,当然也可以自定义表单验证
第二、简单了解学习anjularjsz自带的表单验证
1.必填验证:required,直接添加required属性即可
2.最小长度:ng-minlength,使用ng-minlength=“最小长度值”
3.最大长度:ng-maxlength,使用ng-maxlength=“最大长度值”
4.模式匹配:ng-pattern,使用ng-pattren="模式匹配的正则表达式"
5.邮箱:email,使用直接给文本框的type属性值赋值为email即可--type="email"
6.数字:number,使用直接给文本框的type属性值赋值为number即可--type="number"
7.网页地址:url,使用直接给文本框的type属性值赋值为url即可--type="url"
第四、表单中的控制变量
1.表单的属性值访问方式为:表单名称.文本框名称.属性名称
2.表单验证中用到的表单属性包括如下:
未修改的表单:属性名称关键词【pristine】,bool类型,如果为修改为ture
用户修改过的表单:属性关键词【dirty】,bool类型,只有修改了就为true
合法的表单:属性关键词【valid】,bool类型,只有当表单内容合法才为true
不合法表单:属性关键词【invalid】,bool类型,只要有不合法的都为true
错误:属性关键词【error】,bool类型,只要有不合法的都为true
第五、简单实现注册页面的表单验证
在实现的方式上,根据不同的体验,大致有三种方式
其一、对表单输入实时验证,只有表单验证都通过,才提交表单
实现方式:通过控制提交按钮的可用性来实现
ng-disabled="loginForm.$invalid"
下面举例一个练习实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.oneCount {
width:1000px;
height:60px;
line-height:60px;
border-bottom:1px solid blue;
}
.oneCount .titleCount {
float: left;
width: 150px;
text-align: right;
} .oneCount .valueCount {
float: left;
width: 300px;
text-align: right;
}
.oneCount .valueCount input {
width: 300px;
} .oneCount .alertCount {
float: left;
width: 520px;
margin-left:20px;
} .oneCount .alertCount span{
float: left;
margin-left: 10px;
color:#ff0000;
} .success {
color:#19e1cf !important;
} input .ng-pristine {
color: #808080;
border-bottom: 1px solid #ff0000;
}
input .ng-dirty {
color: #000000;
}
input .ng-valid {
color: #000000;
}
input .ng-invalid {
color: #ff0000;
}
</style>
</head>
<body ng-app="myApp" ng-controller="myContro">
<form name="loginForm" novalidate ng-submit="submitForm()">
<div class="oneCount">
<div class="titleCount">账号:</div>
<div class="valueCount">
<input type="text" name="acount" ng-model="user.acount" placeholder="必填:账号必须由数字字母组合,长度在6-20"
required="required" ng-minlength="6" ng-maxlength="20" ng-pattern="/^[0-9a-zA-Z]+$/"/>
</div>
<div class="alertCount">
<span class="warning">*</span>
<span class="warning" ng-show="loginForm.acount.$dirty&&loginForm.acount.$error.required">acount必填</span>
<span class="warning" ng-show="loginForm.acount.$error.minlength">最少长度为6</span>
<span class="warning" ng-show="loginForm.acount.$error.maxlength">最大长度为20</span>
<span class="warning" ng-show="loginForm.acount.$error.pattern">账号格式不符合要求(只能由数字和字母组成)</span>
<span class="success" ng-show="loginForm.acount.$dirty&&loginForm.acount.$valid">账号输入正确</span>
</div>
</div>
<div class="oneCount">
<div class="titleCount">姓名:</div>
<div class="valueCount">
<input type="text" name="name" ng-model="user.name" placeholder="请输入姓名" ng-maxlength="20" />
</div>
<div class="alertCount">
<span class="warning" ng-show="loginForm.name.$error.maxlength">姓名最大长度为20</span>
<span class="success" ng-show="loginForm.name.$dirty&&loginForm.name.$valid">姓名输入正确</span>
</div>
</div>
<div class="oneCount">
<div class="titleCount">年龄:</div>
<div class="valueCount">
<input type="number" name="age" ng-model="user.age" placeholder="请输入年龄" />
</div>
</div>
<div class="oneCount">
<div class="titleCount">密码:</div>
<div class="valueCount">
<input type="password" name="pass" ng-model="user.pass" placeholder="必填:请输入密码,密码长度在6-20"
required="required" ng-minlength="6" ng-maxlength="20" />
</div>
<div class="alertCount">
<span class="warning">*</span>
<span class="warning" ng-show="loginForm.pass.$dirty&&loginForm.pass.$error.required">密码必填</span>
<span class="warning" ng-show="loginForm.pass.$error.minlength">最少长度为6</span>
<span class="warning" ng-show="loginForm.pass.$error.maxlength">最大长度为20</span>
<span class="success" ng-show="loginForm.pass.$dirty&&loginForm.pass.$valid">密码输入正确</span>
</div>
<div class="oneCount">
<div class="titleCount"></div>
<div class="valueCount">
<input type="submit" value="提交" ng-disabled="loginForm.$invalid" style="width:40px;" />
</div>
</div>
</form>
</body>
</html>
<script src="Scripts/angular.js"></script>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("myContro", function ($scope) {
$scope.user = {
acount: "",
name: "",
age: "",
pass: "",
rePass: ""
}; //提交表单接受函数
$scope.submitForm = function () {
//// 表单真实提交逻辑
alert("提交表单");
}
}); </script>
其二、先触发提交表单事件,在统一对表单数据验证,只有同验证通过才提交数据到后台处理
实现方式:可以给表单添加一个属性,初始化赋值为false,只有提交后才赋值为ture
验证结果提示信息,只有该属性值为true,才显示显示错误信息
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.oneCount {
width:1000px;
height:60px;
line-height:60px;
border-bottom:1px solid blue;
}
.oneCount .titleCount {
float: left;
width: 150px;
text-align: right;
} .oneCount .valueCount {
float: left;
width: 300px;
text-align: right;
}
.oneCount .valueCount input {
width: 300px;
} .oneCount .alertCount {
float: left;
width: 520px;
margin-left:20px;
} .oneCount .alertCount span{
float: left;
margin-left: 10px;
color:#ff0000;
} .success {
color:#19e1cf !important;
} input .ng-pristine {
color: #808080;
border-bottom: 1px solid #ff0000;
}
input .ng-dirty {
color: #000000;
}
input .ng-valid {
color: #000000;
}
input .ng-invalid {
color: #ff0000;
}
</style>
</head>
<body ng-app="myApp" ng-controller="myContro">
<form name="loginForm" novalidate ng-submit="submitForm()">
<div class="oneCount">
<div class="titleCount">账号:</div>
<div class="valueCount">
<input type="text" name="acount" ng-model="user.acount" placeholder="必填:账号必须由数字字母组合,长度在6-20"
required="required" ng-minlength="6" ng-maxlength="20" ng-pattern="/^[0-9a-zA-Z]+$/"/>
</div>
<div class="alertCount">
<span class="warning">*</span>
<span class="warning" ng-show="loginForm.submitted&&loginForm.acount.$dirty&&loginForm.acount.$error.required">acount必填</span>
<span class="warning" ng-show="loginForm.submitted&&loginForm.acount.$error.minlength">最少长度为6</span>
<span class="warning" ng-show="loginForm.submitted&&loginForm.acount.$error.maxlength">最大长度为20</span>
<span class="warning" ng-show="loginForm.submitted&&loginForm.acount.$error.pattern">账号格式不符合要求(只能由数字和字母组成)</span>
<span class="success" ng-show="loginForm.submitted&&loginForm.acount.$dirty&&loginForm.acount.$valid">账号输入正确</span>
</div>
</div>
<div class="oneCount">
<div class="titleCount">姓名:</div>
<div class="valueCount">
<input type="text" name="name" ng-model="user.name" placeholder="请输入姓名" ng-maxlength="20" />
</div>
<div class="alertCount">
<span class="warning" ng-show="loginForm.submitted&&loginForm.name.$error.maxlength">姓名最大长度为20</span>
<span class="success" ng-show="loginForm.submitted&&loginForm.name.$dirty&&loginForm.name.$valid">姓名输入正确</span>
</div>
</div>
<div class="oneCount">
<div class="titleCount">年龄:</div>
<div class="valueCount">
<input type="number" name="age" ng-model="user.age" placeholder="请输入年龄" />
</div>
</div>
<div class="oneCount">
<div class="titleCount">密码:</div>
<div class="valueCount">
<input type="password" name="pass" ng-model="user.pass" placeholder="必填:请输入密码,密码长度在6-20"
required="required" ng-minlength="6" ng-maxlength="20" />
</div>
<div class="alertCount">
<span class="warning">*</span>
<span class="warning" ng-show="loginForm.submitted&&loginForm.pass.$dirty&&loginForm.pass.$error.required">密码必填</span>
<span class="warning" ng-show="loginForm.submitted&&loginForm.pass.$error.minlength">最少长度为6</span>
<span class="warning" ng-show="loginForm.submitted&&loginForm.pass.$error.maxlength">最大长度为20</span>
<span class="success" ng-show="loginForm.submitted&&loginForm.pass.$dirty&&loginForm.pass.$valid">密码输入正确</span>
</div>
<div class="oneCount">
<div class="titleCount"></div>
<div class="valueCount">
<input type="submit" value="提交" style="width:40px;" />
</div>
</div>
</div>
</form>
</body>
</html>
<script src="Scripts/angular.js"></script>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("myContro", function ($scope) {
$scope.user = {
acount: "",
name: "",
age: "",
pass: "",
rePass: ""
};
$scope.submitted = false;
//提交表单接受函数
$scope.submitForm = function () {
if ($scope.loginForm.$valid) {
//// 表单数据真实提交逻辑
} else {
$scope.loginForm.submitted = true;
}
}
}); </script>
其三、失去焦点后验证
时间不早了,明天在仔细研究该问题
今天就到此为止,明天继续研究表单验证,明天学习包括如下几点
表单失去焦点的验证
表单提示信息展示优化,以便提高代码复用性
自定义表单验证
angularjs学习第四天笔记(第一篇:简单的表单验证)的更多相关文章
- 第一百五十四节,封装库--JavaScript,表单验证--提交验证
封装库--JavaScript,表单验证--提交验证 将表单的所有必填项,做一个判断函数,填写正确时返回布尔值 最后在提交时,判断每一项是否正确,全部正确才可以 提交 html <div id= ...
- jQuery笔记(五)jQuery表单验证
前言 上次我们说完jQuery中的动画之后,我们再来看一种jQuery在Web网页应用最为广泛的一种形式,这就是jQuery对表单的操作,通过jQuery对表单的操作,可以有效的提高用户体验.在此之前 ...
- angularjs学习第五天笔记(第二篇:表单验证升级篇)
您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...
- angular学习笔记(二十)-表单验证
本篇主要介绍angular中的表单验证: 表单验证主要有以下一些内容: 1. required指令: 相当于html5的required属性,验证不能为空 2. ng-maxlength属性: 验证内 ...
- ActionBarSherlock学习笔记 第一篇——部署
ActionBarSherlock学习笔记 第一篇--部署 ActionBarSherlock是JakeWharton编写的一个开源框架,使用这个框架,可以实现在所有的Android ...
- angularjs学习第三天笔记(过滤器第二篇---filter过滤器及其自定义过滤器)
您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...
- 《javascript权威指南》读书笔记——第一篇
<javascript权威指南>读书笔记——第一篇 金刚 javascript js javascript权威指南 由于最近想系统学习下javascript,所以开始在kindle上看这本 ...
- Scala语言笔记 - 第一篇
目录 Scala语言笔记 - 第一篇 1 基本类型和循环的使用 2 String相关 3 模式匹配相关 4 class相关 5 函数调用相关 Scala语言笔记 - 第一篇 最近研究了下scala ...
- Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介)
原文:Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介) 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...
随机推荐
- 11. English vocabulary 英语词汇量
11. English vocabulary 英语词汇量 (1) The exact number of English words is not known.The large dictionari ...
- Codeforces Round #547 (Div. 3) G 贪心
https://codeforces.com/contest/1141/problem/G 题意 在一棵有n个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来 ...
- Docker 启动不了容器的问题
今天在运行 docker 的时候,就是执行 docker exec 命令的时候,发现一直报错.具体的报错信息如下: Error response from daemon: Container XXX ...
- 消息中间件——RabbitMQ
RabbitMQ介绍 RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.RabbitM ...
- 在.NET Core中三种实现“可插拔”AOP编程方式(附源码)
一看标题肯定会联想到使用动态编织的方式实现AOP编程,不过这不是作者本文讨论的重点. 本文讨论另外三种在netcore中可实现的方式,Filter(过滤器,严格意义上它算是AOP方式),Dynamic ...
- rand_1tom 产生 rand_1ton
给定一个等概率随机产生1~M的随机函数rand1ToM如下: public int rand1ToM(int m) { return (int) (Math.random() * m) + 1; } ...
- 背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调
[源码下载] 背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调 作者: ...
- C#中关于as关键字的使用
我们在实际编码中有时会用到as关键字来将对象转换为指定类型,与is类型不同的是,is关键字是用于检查对象是否与给定类型兼容,如果兼容就返回true,如果不兼容就返回false.而as关键字会直接进行类 ...
- 往github提交代码流程
一 首先在Github新建一个仓库,回到首页,点击右上角的New repository新建仓库. 二 在本地依次使用下面命令 …or create a new repository on the c ...
- MySQL笔记(7)---事务
1.前言 前面具体讲了MySQL中的锁实现的方式,解释了是如何保证数据在并发情况下的可靠性,并提到了事务REPETABLE READ和READ COMMITTED,解释了一下这两种事务的不同.本章讲具 ...