[Angular] Using directive to create a simple Credit card validator
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的更多相关文章
- [Angular] Create a simple *ngFor
In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...
- [Angular] Custom directive Form validator
Create a directive to check no special characters allowed: import {Directive, forwardRef} from '@ang ...
- [Angular] Export directive functionalities by using 'exportAs'
Directive ables to change component behaives and lookings. Directive can also export some APIs which ...
- [Angular 2] Directive intro and exportAs
First, What is directive, what is the difference between component and directive. For my understandi ...
- 关于angular 自定义directive
关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp& ...
- Create a simple REST web service with Python--转载
今日尝试用python建立一个restful服务. 原文地址:http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-r ...
- [Angular] Test Directive
directive: import { Directive, HostListener, HostBinding, ElementRef } from '@angular/core'; @Direct ...
- 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 ...
- Create a Bootable MicroSD Card
http://gumstix.org/create-a-bootable-microsd-card.html Create a Bootable MicroSD Card Beginners Note ...
随机推荐
- 【例题 8-5 UVA - 11054】Wine trading in Gergovia
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑第一个店. 如果它要酒的话,只能从第二个店那里运过来. 这样,问题就转化成后n-1个店的问题了. 然后会发现,第二家店它的情况也 ...
- HDU——T 3579 Hello Kiki
http://acm.hdu.edu.cn/showproblem.php?pid=3579 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- 杭电1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Beginning iOS Programming
Beginning iOS Programming 2014年 published by Wrox
- grep 过滤器基础
grep是一种文本搜索工具,能使用正则表达式搜索文本,并把匹配的行显示出来. grep 选项 模式 文件 grep "bash" /etc/passwd 将/etc/passwd下 ...
- Ubuntu14.04中踩过的坑
今天安装Ubuntu 14.0.4,因为需要使用python3,所以就直接配置如下:sudo rm /usr/bin/pythonsudo ln -s /usr/bin/python3.5 /usr ...
- jfreechart,pdf生成组件iText,jasper report报表组件及POI操作excel等在企业软件开发中常遇到的第三方应用
熟悉WEB Service ,Ajax,DWR,JQuery,iBatis等技术,熟练TOMCAT,IIS,JBoss,WebLogic等服务器 图表组件JFreeChart PDF组件-iText的 ...
- AlertDialog的onCreateDialog与onPrepareDialog用法
场景:在一个Activity中多次使用弹出对话框.而且对话框携带着动态变化的信息数据,这时假设仅仅使用onCreateDialog(int id, Bundle bundle)回调,则会发现第一次以后 ...
- Oracle 带回滚的存储过程
create or replace procedure PROC_insertUserAmount ( userid number, msgtype number, amountvalue numbe ...
- 10.9 android输入系统_APP跟输入系统建立联系和Dispatcher线程_分发dispatch
12. 输入系统_APP跟输入系统建立联系_InputChannel和Connection核心: socketpair // 第9课第3节_输入系统_必备Linux编程知识_任意进程双向通信(scok ...