您好,我是一名后端开发工程师,由于工作需要,现在系统的从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学习第四天笔记(第一篇:简单的表单验证)的更多相关文章

  1. 第一百五十四节,封装库--JavaScript,表单验证--提交验证

    封装库--JavaScript,表单验证--提交验证 将表单的所有必填项,做一个判断函数,填写正确时返回布尔值 最后在提交时,判断每一项是否正确,全部正确才可以 提交 html <div id= ...

  2. jQuery笔记(五)jQuery表单验证

    前言 上次我们说完jQuery中的动画之后,我们再来看一种jQuery在Web网页应用最为广泛的一种形式,这就是jQuery对表单的操作,通过jQuery对表单的操作,可以有效的提高用户体验.在此之前 ...

  3. angularjs学习第五天笔记(第二篇:表单验证升级篇)

    您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...

  4. angular学习笔记(二十)-表单验证

    本篇主要介绍angular中的表单验证: 表单验证主要有以下一些内容: 1. required指令: 相当于html5的required属性,验证不能为空 2. ng-maxlength属性: 验证内 ...

  5. ActionBarSherlock学习笔记 第一篇——部署

    ActionBarSherlock学习笔记 第一篇--部署          ActionBarSherlock是JakeWharton编写的一个开源框架,使用这个框架,可以实现在所有的Android ...

  6. angularjs学习第三天笔记(过滤器第二篇---filter过滤器及其自定义过滤器)

    您好,我是一名后端开发工程师,由于工作需要,现在系统的从0开始学习前端js框架之angular,每天把学习的一些心得分享出来,如果有什么说的不对的地方,请多多指正,多多包涵我这个前端菜鸟,欢迎大家的点 ...

  7. 《javascript权威指南》读书笔记——第一篇

    <javascript权威指南>读书笔记——第一篇 金刚 javascript js javascript权威指南 由于最近想系统学习下javascript,所以开始在kindle上看这本 ...

  8. Scala语言笔记 - 第一篇

    目录 Scala语言笔记 - 第一篇 1 基本类型和循环的使用 2 String相关 3 模式匹配相关 4 class相关 5 函数调用相关 Scala语言笔记 - 第一篇 ​ 最近研究了下scala ...

  9. Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介)

    原文:Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介) 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...

随机推荐

  1. 常见bat(批处理)命令的语法规则

    最近由于在做cocos2d的项目,需要用到一些bat命令,在此做些记录. bat命令用txt文本编辑就行,编辑完之后将后缀名改为bat即可运行.先来一个最简单的例子: @echo off echo \ ...

  2. 曙光服务器挂载EMC存储

    1.登录集群(用户名密码远程登录,然后切换到root用户) 2.连接主机:ssh node72 3.在主机下进行存储挂载: 1)fdisk -l 查看磁盘信息,如下图所示: 2)查看磁盘挂载信息:mo ...

  3. Oracle存储过程,游标使用

    Oracle存储过程: 语法: CREATE [OR REPLACE] PROCEDURE procedure_name (arg1 [mode1] datatype1,arg2 [mode2] da ...

  4. oracle-查询-时间条件查询

    select * from 表名 where date =to_date('时间','yyyy-dd-mm');

  5. WITH RECOMPILE 和 OPTION(RECOMPILE) 使用上的区别

    在考虑重编译T-SQL(或者存储过程)的时候,有两种方式可以实现强制重编译(前提是忽略导致重编译的其他因素的情况下,比如重建索引,更新统计信息等等), 一是基于WITH RECOMPILE的存储过程级 ...

  6. CVE-2015-1641 Office类型混淆漏洞及shellcode分析

    作者:枕边月亮 原文来自:CVE-2015-1641 Office类型混淆漏洞及shellcode分析 0x1实验环境:Win7_32位,Office2007 0x2工具:Windbg,OD,火绒剑, ...

  7. WebView 错误码整理

    在使用WebView中,我们不可避免的会接触到WebView加载失败的异常处理的需求,这时候,需要我们监听失败的方法也就是onReceivedError方法: public class CustomW ...

  8. JavaScript中的三种弹出框的区别与使用

    JavaScript中有三种原生的弹出框,分别是alert.confirm.prompt.分别表示弹出框.确认框.信息框. 以下是示例代码: <!DOCTYPE html> <htm ...

  9. JS应用实例5:全选、动态添加

    HTML代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  10. Swift5 语言指南(十) 枚举

    一个枚举定义了一个通用型的一组相关的值,使你在你的代码中的一个类型安全的方式这些值来工作. 如果您熟悉C,您将知道C枚举将相关名称分配给一组整数值.Swift中的枚举更灵活,并且不必为枚举的每个案例提 ...