We will use 'HostListener' and 'HostBinding' to accomplish the task.

The HTML:

      <label>
Credit Card Number
<input
name="credit-card"
type="text"
credit-card
placeholder="Enter your 16-digit card number">
</label>

Create directive:

import { Directive, ElementRef, HostListener, HostBinding } from '@angular/core';

@Directive({
selector: '[credit-card]'
})
export class CreditCardDirective {
constructor(private element: ElementRef) {}
}

Add a HostListener when user type input:

And we want that the max length of string user type is 16 and it should be formatted as '6666 6666 6666 6666'.

  @HostListener('input', ['$event'])
onKeyDown(event: KeyboardEvent) {
this.border = "";
const input = event.target as HTMLInputElement; // the length should be no more than 16
let trimmed = input.value.replace(/\s+/g, '');
if(trimmed.length > 16) {
trimmed = trimmed.substr(0, 16);
} // should be 6666 6666 6666 6666
let numbers = [];
for(let i = 0; i < trimmed.length; i +=4) {
numbers.push(trimmed.substr(i, 4));
} input.value = numbers.join(' ');
}

Now we want to use @HostBinding to change style props when what user entered is not a number:

  onKeyDown(event: KeyboardEvent) {
this.border = "";
const input = event.target as HTMLInputElement; // the length should be no more than 16
let trimmed = input.value.replace(/\s+/g, '');
if(trimmed.length > 16) {
trimmed = trimmed.substr(0, 16);
} // if we pass in char, show red border
if((/[^\d]/g).test(trimmed)) {
this.border = '1px solid red';
} // should be 6666 6666 6666 6666
let numbers = [];
for(let i = 0; i < trimmed.length; i +=4) {
numbers.push(trimmed.substr(i, 4));
} input.value = numbers.join(' ');
}

[Angular] Using directive to create a simple Credit card validator的更多相关文章

  1. [Angular] Create a simple *ngFor

    In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...

  2. [Angular] Custom directive Form validator

    Create a directive to check no special characters allowed: import {Directive, forwardRef} from '@ang ...

  3. [Angular] Export directive functionalities by using 'exportAs'

    Directive ables to change component behaives and lookings. Directive can also export some APIs which ...

  4. [Angular 2] Directive intro and exportAs

    First, What is directive, what is the difference between component and directive. For my understandi ...

  5. 关于angular 自定义directive

    关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp& ...

  6. Create a simple REST web service with Python--转载

    今日尝试用python建立一个restful服务. 原文地址:http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-r ...

  7. [Angular] Test Directive

    directive: import { Directive, HostListener, HostBinding, ElementRef } from '@angular/core'; @Direct ...

  8. Real Time Credit Card Fraud Detection with Apache Spark and Event Streaming

    https://mapr.com/blog/real-time-credit-card-fraud-detection-apache-spark-and-event-streaming/ Editor ...

  9. Create a Bootable MicroSD Card

    http://gumstix.org/create-a-bootable-microsd-card.html Create a Bootable MicroSD Card Beginners Note ...

随机推荐

  1. listctrl调整表头高度

    CListCtrl派生类下CMyListCtrl.h class CMyListCtrl :public CListCtrl { public: // 设置表头高度 void SetHeadHeigh ...

  2. mysql测试spring事务是否生效

    同时对三张表进行插入操作,事务保证完整性.下面进行简单测试: 1. 锁定表 锁定用户表 LOCK TABLES user WRITE; 查看表是否锁定: show ; 显示被锁定的表. 2. 验证在同 ...

  3. JS 保留2位小数 四舍五入(小数点后面不足2位,自动用0补齐)

    function changeTwoDecimal_f(x) { var f_x = parseFloat(x); if (isNaN(f_x)) { alert('function:changeTw ...

  4. 在MacOS下使用Fiddler抓包

    在MacOS下使用Fiddler抓包 有两种方式,分别是安装Mac版的Fiddler,或者是用虚拟机,安装Windows系统,在Windows系统下运行Fiddler对Mac系统中的内容进行抓包. M ...

  5. 【Codeforces Round #432 (Div. 2) A】 Arpa and a research in Mexican wave

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] t<=k,输出t t>=n,输出k-t+n 其他情况都是k [错的次数] 0 [反思] 在这了写反思 [代码] /* */ #in ...

  6. 展示C代码覆盖率的gcovr工具简单介绍及相关命令使用演示样例

    (本人正在參加2015博客之星评选,诚邀你来投票,谢谢:username=zhouzxi">http://vote.blog.csdn.net/blogstar2015/candida ...

  7. C++开源码项目汇总

    Google的C++开源码项目 v8  -  V8 JavaScript Engine V8 是 Google 的开源 JavaScript 引擎. V8 採用 C++ 编写,可在谷歌浏览器(来自 G ...

  8. bootstrap课程4 bootstrap的css样式有哪些内容需要注意

    bootstrap课程4 bootstrap的css样式有哪些内容需要注意 一.总结 一句话总结: 1.如何选择产品(框架)的版本? 大版本下的最后一个版本,但是同时又要选择稳定的版本,也就是如果做产 ...

  9. ArcGIS中ObjectID,FID和OID字段区别

    lysc_forever 原文 ArcGIS中ObjectID,FID和OID字段有什么区别 ArcGIS Desktop 独立的表和属性表都有一个ObjectID字段.这个字段中包含一个唯一的,长整 ...

  10. proxool数据库连接池用法

    今天给大家介绍一种新的数据连接池实现方式--proxool数据库连接池,这是一个健壮.易用的连接池.以下通过一个Demo说明一下怎样使用: 项目结构例如以下: DBLink.java文件里的代码: p ...