[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 ...
随机推荐
- oracle job有定时执行功能
oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的 ...
- Redis封装之Hash
RedisHashService: /// <summary> /// Hash:类似dictionary,通过索引快速定位到指定元素的,耗时均等,跟string的区别在于不用反序列化,直 ...
- JavaScript Debug调试技巧
收藏于:https://blog.fundebug.com/2017/12/04/javascript-debugging-for-beginners/
- K-近邻算法学习
# -- coding: utf-8 -- from numpy import * import operator def createDataSet(): group = array([[1.0,1 ...
- 【VC++学习笔记三】控件自绘
MFC应用程序中,大部分的控件类型都已经被定制好了,即便是修改,也只是小范围内的修改,而很多情况下,我们又需要对界面进行特殊定制,这时,最好的办法就是用CWnd类进行派生,自己生成新的窗体,在WM_P ...
- Top 16 Java 应用类 - 这些功能再也不用自己写了
Java中有很多应用类.这些类定义静态方法能够解决非常多常见的问题.以下是通过5万个开源项目统计得到的最热门的16个应用类. 类按热门程序排列.类的方法也是按热门程序排序. 浏览这个类能够看看有哪些功 ...
- 中科燕园GIS外包--移动GIS
移动GIS恰逢其时 得益于移动智能终端的普及和移动互联网的发展,伴随着GIS技术的发展和应用的深入.越来越多的企业和普通消费者開始体会到移动GIS的巨大潜力和价值. 移动GIS轻便灵活,受众面广.随时 ...
- adb logcat 使用
之前打印log的时候,使用的是别人配置好的快捷键,结果现在快捷键没有配置,具体的log命令就不会了.今天上网查了一下,记录下来 打印的log是 android.util.Log.e("zha ...
- OpenCV特征点检测——Surf(特征点篇)&flann
学习OpenCV--Surf(特征点篇)&flann 分类: OpenCV特征篇计算机视觉 2012-04-20 21:55 19887人阅读评论(20)收藏举报 检测特征 Surf(Spee ...
- 简单说一下 JSON和JSONP
JSON和JSONP,但从缩写看,可能会以为他们是很相似的两个名词,但他们除了缩写相似外,他们是两种类型的概念. 首先: JSON(JavaScript Object Notation)即JavaSc ...