Angular表单的本地校验和远程校验
AngularJS Form 进阶:远程校验和自定义输入项
表单远程校验
HTML代码:
- <!doctype html>
- <html ng-app="form-example1">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
- <script src="../angular-1.0.3/angular.js"></script>
- <script src="FormValidation.js"></script>
- </head>
- <body>
- <div>
- <form name="myForm" class="css-form" novalidate>
- <div>
- 整数(0-10):
- <input type="number" ng-model="size" name="size" min="0" max="10" integer/>
- {{size}}
- <br/>
- <span ng-show="myForm.size.$error.integer">不是合法的整数!</span>
- <span ng-show="myForm.size.$error.min || myForm.size.$error.max">
- 数值必须位于0到10之间!
- </span>
- </div>
- <div>
- 浮点数:
- <input type="text" ng-model="length" name="length" smart-float />
- {{length}}
- <br/>
- <span ng-show="myForm.length.$error.float">不是合法的浮点数!</span>
- </div>
- <div>
- 远程校验:
- <input type="text" ng-model="remote" name="remote" remote-validation />
- {{remote}}
- <br/>
- <span ng-show="myForm.remote.$error.remote">非法数据!</span>
- </div>
- </form>
- </div>
- </body>
- </html>
JS代码:
- var app = angular.module('form-example1', []);
- var INTEGER_REGEXP = /^\-?\d*$/;
- app.directive('integer', function() {
- return {
- require : 'ngModel',
- link : function(scope, elm, attrs, ctrl) {
- ctrl.$parsers.unshift(function(viewValue) {
- if (INTEGER_REGEXP.test(viewValue)) {
- ctrl.$setValidity('integer', true);
- return viewValue;
- } else {
- ctrl.$setValidity('integer', false);
- return undefined;
- }
- });
- }
- };
- });
- var FLOAT_REGEXP = /^\-?\d+((\.|\,)\d+)?$/;
- app.directive('smartFloat', function() {
- return {
- require : 'ngModel',
- link : function(scope, elm, attrs, ctrl) {
- ctrl.$parsers.unshift(function(viewValue) {
- if (FLOAT_REGEXP.test(viewValue)) {
- ctrl.$setValidity('float', true);
- return parseFloat(viewValue.replace(',','.'));
- } else {
- ctrl.$setValidity('float', false);
- return undefined;
- }
- });
- }
- };
- });
- app.directive('remoteValidation', function($http) {
- return {
- require : 'ngModel',
- link : function(scope, elm, attrs, ctrl) {
- elm.bind('keyup', function() {
- $http({method: 'GET', url: 'FormValidation.jsp'}).
- success(function(data, status, headers, config) {
- if(parseInt(data)==0){
- ctrl.$setValidity('remote',true);
- }else{
- ctrl.$setValidity('remote',false);
- }
- }).
- error(function(data, status, headers, config) {
- ctrl.$setValidity('remote', false);
- });
- });
- }
- };
- });
Angular表单的本地校验和远程校验的更多相关文章
- ngVerify - 更高效的 angular 表单验证
ngVerify v1.5.0 a easy Angular Form Validation plugin.简洁高效的__angular表单验证插件__ See how powerful it.看看它 ...
- Angular 表单(二) - 模板驱动表单
import { Component, OnInit } from '@angular/core'; import { Hero} from '../hero'; @Component({ selec ...
- angular表单经验分享
原文 https://www.jianshu.com/p/af359bd04f32 大纲 1.表单的名字不可以和传入的值的名字相同 2.在模板驱动表单的时候要使用angular表单的验证功能,需要将n ...
- Angular 表单验证类库 ngx-validator 1.0 正式发布
背景介绍 之前写了一篇 <如何优雅的使用 Angular 表单验证>,结尾处介绍了统一验证反馈的类库 ngx-validator ,由于这段时间一直在新模块做微前端以及相关业务组件库, ...
- Angular表单 (一)表单简介
Angular 表单 angular提供了两种不同的方法来通过表单处理用户输入:响应式表单和模板驱动表单.二者都从视图中捕获用户输入事件.验证用户输入.创建表单模型.修改数据模型,并提供跟踪这些更改的 ...
- 如何优雅的使用 Angular 表单验证
随便说说,这一节可以跳过 去年参加 ngChine 2018 杭州开发者大会的时候记得有人问我: Worktile 是什么时候开始使用 Angular 的,我说是今年(2018年) 3 月份开始在新模 ...
- angular表单的使用实例
原文 https://www.jianshu.com/p/da1fd5396798 大纲 1.模板驱动表单的创建 2.响应式表单的创建 3.模板驱动型表单的自定义指令 4.响应式表单的自定义指令 5. ...
- angular表单知识点
原文 https://www.jianshu.com/p/c772d143e1fc 大纲 1.对表单的理解 2.模板驱动表单(Template Driven Forms) 3.响应式表单(Reacti ...
- Angular表单验证
novalidate 去掉html5自带的验证 ng-minlength 规定输入文本的最小长度 ng-maxlength 规定输入文本的最大长度 ng-submit 接收一个方法名 ...
随机推荐
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 4)
(五)列表查询中的阈值限制 在之前版本的SharePoint 中,如果在查询的时候没有指定返回数目,那么SharePoint将会查找该列表中所有的条目,这可能会造成在SQL表中需要返回大量的条目,极大 ...
- JVM 的 Xms 和 Xmx 设置一样大小的内存容量
4. [推荐]在线上生产环境,JVM 的 Xms 和 Xmx 设置一样大小的内存容量,避免在 GC 后调整堆 大小带来的压力. 现在,我们知道了Elasticsearch所公开的缓存和缓冲区,可以尝试 ...
- DevExpress控件-lookupedit的使用方法详解(图文)转http://blog.csdn.net/qq395537505/article/details/50920508
绑定数据源: lookupedit.Properties.ValueMember = 实际要用的字段; //相当于editvalue lookupedit.Properties.DisplayMemb ...
- VCL控件组件大都应该重载TWinControl的虚函数WndProc来进行处理窗口消息的工作
TWinControl的构造函数中会调用MakeObjectInstance并且传递MainWndProc作为窗口消息处理函数,而MainWndProc则会调用虚函数WndProc来处理窗口消息.留个 ...
- ExtJS4 给同一个formpanel不同的url
formpanel能够这样使用,api上的样例: var panel=Ext.create('Ext.form.Panel', { title: 'Simple Form', bodyPadding: ...
- linux 文件和文件夹的ctime,mtime,atime的差别
多了不再赘述,看以下解释 st_atime Time when file data was last accessed. Changed by the f ...
- Activity重要函数
一.onConfigurationChanged 与 android:configChanges Lists configuration changes that the activity will ...
- 20160422 --Switch…case 总结; 递归算法
13 2016-04-22 11:01:00 Switch…case 总结(网摘) 例题: Console.WriteLine("1.汉堡包"); Console.WriteLi ...
- KVM虚拟化虚拟机支持虚拟化
一.开启的时候需要关闭所有虚拟机: 首先检查 KVM host(宿主机/母机)上的kvm_intel模块是否打开了嵌套虚拟机功能(默认是开启的): 1.modinfo kvm_intel | grep ...
- 【鸟哥的Linux私房菜】笔记3
正确地开机 最好不要使用root账号登陆!GNOME图形界面 View items as a list X WindowShell 文本交互界面bash是Shell的名称,Linux的默认壳程序就是b ...