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 ...
随机推荐
- sleep() 和 wait() 区别
sleep()不释放同步锁,wait()释放同步锁 sleep()的作用是让线程休眠指定的时间,时间到后自动恢复线程执行.运行的主动权是由线程决定的. wait()可以用notify()直接唤起,运行 ...
- Java 装箱和拆箱
1.装箱机制 基础类型引用到其包装类型,这样就可以调用其各种方法. 例如,我们声明: Integer a = 1; 其在编译过程中会自动解释成: Integer a = Integer.valueOf ...
- SSM_CRUD新手练习(7)Spring单元测试分页请求
好久没写这个系列博客了是因为本人去公司实习去了,公司用的是Spring+SpingMvc+Hibernate现在有时间了不管怎么样继续把这个项目写完. 因为机器的原因,我的环境变成了IDEA+orac ...
- 为opencv添加contrib库
自从进入3.X时代以后,OpenCV将代码库分成了两部分,分别是稳定的核心功能库和试验性质的contrib库,之前已经讲过opencv的核心库的安装,现在讲解一下其附带的依赖库的安装. 一.Cmake ...
- .NET Core微服务之路:基于Consul最少集群实现服务的注册与发现(二)
重温Consul最少化集群的搭建
- rabbitMq 初步
RabbitMQ的工作原理 它的基本结构 组成部分说明如下: Broker:消息队列服务进程,此进程包括两个部分:Exchange和Queue. Exchange:消息队列交换机,按一定的规则将消息路 ...
- 枚举类型enum详解——C语言
enum enum是C语言中的一个关键字,enum叫枚举数据类型,枚举数据类型描述的是一组整型值的集合(这句话其实不太妥当),枚举型是预处理指令#define的替代,枚举和宏其实非常类似,宏在预处理阶 ...
- 你可能不知道的BFC在实际中的应用
概述 BFC是块级格式化上下文,它的一个令人熟知的运用是双飞翼布局或者两列布局.但其实它在其它地方也有很巧妙的运用.我把研究的心得记录下来,供以后开发时参考,相信对其他人也有用. 参考资料: mdn块 ...
- Java学习笔记36(File类)
File类可以对操作系统中的文件进行操作: File类的静态成员变量: package demo; import java.io.File; public class FileDemo { publi ...
- python实现快速排序算法
快速排序算法又称划分交换排序(partition-exchange sort),一种排序算法,最早由东尼·霍尔提出.在平均状况下, 排序n个项目要O(nlogn)次比较.在最坏状况下则需要O(n*2) ...