[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 ...
随机推荐
- OPENCV(6) —— 角点检测
图像特征的类型通常指边界.角点(兴趣点).斑点(兴趣区域).角点就是图像的一个局部特征,应用广泛.harris角点检测是一种直接基于灰度图像的角点提取算法,稳定性高,尤其对L型角点检测精度高,但由于采 ...
- 简单STL笔记
想了好久,还是把自己了解的先整理一下吧,毕竟老是忘,这里主要简单介绍三种容器 set,queue,vector,以及栈 stack,队列queue 的简单用法.一.set 在set中,效率比vecto ...
- python获取教务管理系统的MM照片
前提:你的教务管理系统是明文存储大家的图片,加密的图片就不好弄了... 也就是能够通过浏览器直接访问...技术上就不存在什么问题了...如果是学号存储的就更方便了 然后我们就可以写个小脚本了...其实 ...
- 关于IDEA无法完整显示项目文件结构
今天发现一个奇怪的问题,就是我从本地导入了文件,明明已经成功了,但是在我的项目结构里面就是不显示,然而点击目录, 又能打开相应的文件,如图所示: 其实这个打开的只是包文件,只需要如下图操作即可:
- python学习二,字符串常用操作
字符串可以说是在日常开发中应用最广泛的了,现在来总结下有关python中有关字符串一些常用操作 首先我们声明一个字符串变量 str = "hello world" 下面我们来依次介 ...
- poj2486--Apple Tree(树状dp)
Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7789 Accepted: 2606 Descri ...
- 【Swift初见】Swift数组(二)
在苹果的开发文档中对Array还提供了其它的操作算法: 1.Sort函数: 对数组进行排序.依据指定的排序规则,看以下的代码: var array = [2, 3, 4, 5] array.sort{ ...
- 调用google翻译
1. [代码]maven依赖 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <dependency> <groupId>org.a ...
- html的学习思维导图
- 关于cook操作
http://www.cnblogs.com/fishtreeyu/archive/2011/10/06/2200280.html