Skip to content

Commit 09c839e

Browse files
committed
updated Usage section of README; refactored directive implementation in demo
1 parent 5af99a4 commit 09c839e

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,68 @@ Angular2 CC Library - for validation and formating of input parameters
88
3. run `npm run dev`
99
4. visit `http://localhost:4200`
1010

11+
# Usage
12+
13+
## Formating Directive
14+
On the input fields, add the specific directive to format inputs.
15+
All fields must be `type='tel'` in order to support spacing and additional characters
16+
17+
**Credit Card Formater**
18+
* add `ccNumber` directive:
19+
```
20+
<input id="cc-number" type="tel" autocomplete="cc-number" ccNumber>
21+
```
22+
23+
**Expiration Date Formater**
24+
Will support format of MM/YY or MM/YYYY
25+
* add `ccExp` directive:
26+
```
27+
<input id="cc-exp-date" type="tel" autocomplete="cc-exp" ccExp>
28+
```
29+
30+
**CVC Formater**
31+
* add `ccCvc` directive:
32+
```
33+
<input id="cc-cvc" type="tel" autocomplete="off" ccCvc>
34+
```
35+
36+
### Validation
37+
Current only Model Validation is supported.
38+
To implement, import the validator library and apply the specific validator on each form control
39+
```
40+
import { CreditCardValidator } from '../../src/validators/credit-card.validator';
41+
42+
@Component({
43+
selector: 'app',
44+
template: `
45+
<form #demoForm="ngForm" (ngSubmit)="onSubmit(demoForm)" novalidate>
46+
<input id="cc-number" formControlName="creditCard" type="tel" autocomplete="cc-number" ccNumber>
47+
<input id="cc-exp-date" formControlName="expirationDate" type="tel" autocomplete="cc-exp" ccExp>
48+
<input id="cc-cvc" formControlName="cvc" type="tel" autocomplete="off" ccCvc>
49+
</form>
50+
`
51+
})
52+
export class AppComponent implements OnInit {
53+
form: FormGroup;
54+
submitted: boolean = false;
55+
56+
constructor(private _fb: FormBuilder) {}
57+
58+
ngOnInit() {
59+
this.form = this._fb.group({
60+
creditCard: ['', [<any>CreditCardValidator.validateCCNumber]],
61+
expirationDate: ['', [<any>CreditCardValidator.validateExpDate]],
62+
cvc: ['', [<any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(4)]]
63+
});
64+
}
65+
66+
onSubmit(form) {
67+
this.submitted = true;
68+
console.log(form);
69+
}
70+
}
71+
```
72+
1173
# Inspiration
1274

1375
Based on Stripe's [jquery.payment](https://github.com/stripe/jquery.payment) plugin but adapted for use by Angular2
@@ -18,6 +80,7 @@ MIT
1880

1981
# TODO
2082
- [ ] Documentation
21-
- [ ] Tests
83+
- [x] Unit Tests
84+
- [ ] Protractor Tests
2285
- [ ] Validate cvc input length against value from card object
2386
- [ ] publish to NPM

example/src/app.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { Validators, FormControl, FormGroup, FormBuilder } from '@angular/forms';
3-
import { CreditCardValidator } from '../../src/shared/credit-card.validator';
2+
import { Validators, FormGroup, FormBuilder } from '@angular/forms';
3+
import { CreditCardValidator } from '../../src/validators/credit-card.validator';
44

55
@Component({
66
selector: 'app',
@@ -15,7 +15,7 @@ export class AppComponent implements OnInit {
1515
ngOnInit() {
1616
this.form = this._fb.group({
1717
creditCard: ['', [<any>CreditCardValidator.validateCCNumber]],
18-
expirationDate: ['', [<any>CreditCardValidator.validateExp]],
18+
expDate: ['', [<any>CreditCardValidator.validateExpDate]],
1919
cvc: ['', [<any>Validators.required, <any>Validators.minLength(3), <any>Validators.maxLength(4)]] // TODO compare actual results against card type
2020
});
2121
}

example/src/app.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
<p>A general purpose library for building credit card forms, validating inputs and formatting numbers.</p>
33
<div class="form-group">
44
<label for="cc-number" class="control-label">Card number formatting <small class="text-muted">[<span class="cc-brand"></span>]</small></label>
5-
<input id="cc-number" type="tel" class="input-lg form-control cc-number" autocomplete="cc-number" placeholder="•••• •••• •••• ••••" ccNumber>
5+
<input id="cc-number" formControlName="creditCard" type="tel" class="input-lg form-control cc-number" autocomplete="cc-number" placeholder="•••• •••• •••• ••••" ccNumber>
66
</div>
77

88
<div class="form-group">
99
<label for="cc-exp" class="control-label">Card expiry formatting</label>
10-
<input id="cc-exp" type="tel" class="input-lg form-control cc-exp" autocomplete="cc-exp" placeholder="•• / ••" ccExp>
10+
<input id="cc-exp" formControlName="expDate" type="tel" class="input-lg form-control cc-exp" autocomplete="cc-exp" placeholder="•• / ••" ccExp>
1111
</div>
1212

1313
<div class="form-group">
1414
<label for="cc-cvc" class="control-label">Card CVC formatting</label>
15-
<input id="cc-cvc" type="tel" class="input-lg form-control cc-cvc" autocomplete="off" placeholder="•••" required ccCVC>
15+
<input id="cc-cvc" formControlName="cvc" type="tel" class="input-lg form-control cc-cvc" autocomplete="off" placeholder="•••" required ccCVC>
1616
</div>
1717
<button type="submit" class="btn btn-lg btn-primary">Submit</button>
1818
<div class="errors" *ngIf="submitted">

0 commit comments

Comments
 (0)