[Angular] Custom directive Form validator
Create a directive to check no special characters allowed:
import {Directive, forwardRef} from '@angular/core';
import {AbstractControl, NG_VALIDATORS, Validator} from '@angular/forms';
@Directive({
selector: `[formControl][no-special-chars],
[formControlName][no-special-chars],
[ngModel][no-special-chars]`,
providers: [
{
multi: true,
provide: NG_VALIDATORS,
useExisting: forwardRef(() => NoSpecialCharsValidator)
}
]
}) export class NoSpecialCharsValidator implements Validator {
validate(c: AbstractControl): { [key: string]: any; } {
const res = /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(c.value);
return res ? {special: true}: null;
}
}
<div class="meal-form__name">
<label>
<h3>Meal name</h3>
<input type="text"
no-special-chars
formControlName="name"
placeholder="e.g. English Breakfast">
<div class="error" *ngIf="noSpecials">
Cannot contain special characters
</div>
</label>
</div>
get noSpecial() {
return (
this.form.get('name').hasError('special') &&
this.form.get('name').touched
);
}
[Angular] Custom directive Form validator的更多相关文章
- [Angular] Using directive to create a simple Credit card validator
We will use 'HostListener' and 'HostBinding' to accomplish the task. The HTML: <label> Credit ...
- [Angular 2] Directive intro and exportAs
First, What is directive, what is the difference between component and directive. For my understandi ...
- Effective Java 75 Consider using a custom serialized form
Principle Do not accept the default serialized form without first considering whether it is appropri ...
- 关于angular 自定义directive
关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp& ...
- [php基础]PHP Form表单验证:PHP form validator使用说明
在PHP网站开发建设中,用户注册.留言是必不可少的功能,用户提交的信息数据都是通过Form表单提交,为了保证数据的完整性.安全性,PHP Form表单验证是过滤数据的首要环节,PHP对表单提交数据的验 ...
- [Angular] Test Directive
directive: import { Directive, HostListener, HostBinding, ElementRef } from '@angular/core'; @Direct ...
- [Angular] Export directive functionalities by using 'exportAs'
Directive ables to change component behaives and lookings. Directive can also export some APIs which ...
- [Angular] @ViewChild read custom directive and exportAs
For example we have a component: <Card ></Card> And a driective: <Card highlighted> ...
- angular $http 与form表单的select-->refine
<!DOCTYPE html> <html ng-app="a2_15"> <head> <meta http-equiv="C ...
随机推荐
- vim 脚本之快捷注释
今天初步学习了下vim的脚本知识,并尝试写了一个简单的脚本.当然,这个脚本很简单,使用的方法也很笨拙.不过,这仅仅是一个开始,等以后随着对vim语法的深入了解,会不断优化这个脚本的.先记录下来 &qu ...
- Android - TextureView, SurfaceView和GLSurfaceView 以及 SurfaceTexture
这几个概念比较绕, 又比较相近. 初看比较糊涂, 把握关键点就好. 关键字 View SurfaceViewGLSurfaceViewTextureView这三个后缀都是View, 所以这三个东西都是 ...
- thinkphp里面的or查询
thinkphp里面的or查询 whereOr 方法 使用whereOr 方法进行OR 查询: Db::table('think_user') ->where('name','like','%t ...
- zzulioj--1816--矩形(好题数学)
1816: 矩形 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 88 Solved: 24 SubmitStatusWeb Board Descri ...
- poj--2391--Ombrophobic Bovines(floyd+二分+最大流拆点)
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u ...
- spring mvc获取路径参数的几种方式
一.从视图向controller传递值, controller <--- 视图 1.通过@PathVariabl注解获取路径中传递参数 (参数会被复制到路径变量) @RequestMappin ...
- 快速select算法的实现
代码来自: http://blog.csdn.net/v_JULY_v 算法思想: // Quick_select.cpp : 定义控制台应用程序的入口点. // #include "std ...
- 微信、QQ中app的下载问题
最近在做一个项目,有一项功能是从微信中的分享页或者产品推广页面中下载app:在微信中直接下载app时微信是“拒绝”的,所以一般的做法是点击下载按钮弹出遮罩层,提示在浏览器中打开,然后进入外部浏览器,再 ...
- 【Eclipse中使用Git之一】把远程仓库的项目,clone到eclipse里面
[Eclipse中使用Git之一]把远程仓库的项目,clone到eclipse里面 2015-01-29 19:25 15779人阅读 评论(1) 收藏 举报 .embody{ padding:10p ...
- python 服务端判断客户端异常断开
在进行 python 套接字编程时,服务端程序要判断客户端是否异常断开[由于断电或者其他突发情况导致链接中断],可以通过以下几种方式判断: 1.如果通信协议中,设有心跳包,则可记录上次收到时间,将服务 ...