This repository has been archived by the owner on Aug 14, 2021. It is now read-only.
forked from dancannon/payment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
80 lines (59 loc) · 2.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<html>
<head>
<script src="../dist/payment.js"></script>
<style type="text/css" media="screen">
input.invalid {
border: 2px solid red;
}
.validation.failed:after {
color: red;
content: 'Validation failed';
}
.validation.passed:after {
color: green;
content: 'Validation passed';
}
</style>
</head>
<body>
<form novalidate autocomplete="on">
<h2>Card number formatting</h2>
<input type="text" class="cc-number" pattern="\d*" x-autocompletetype="cc-number" placeholder="Card number" required>
<h2>Expiry formatting</h2>
<input type="text" class="cc-exp" pattern="\d*" x-autocompletetype="cc-exp" placeholder="Expires MM/YY" required maxlength="9">
<h2>CVC formatting</h2>
<input type="text" class="cc-cvc" pattern="\d*" x-autocompletetype="cc-csc" placeholder="Security code" required autocomplete="off">
<h2>Restrict Numeric</h2>
<input type="text" data-numeric>
<h2 class="validation"></h2>
<button type="submit">Submit</button>
</form>
<script>
var J = Payment.J,
numeric = document.querySelector('[data-numeric]'),
number = document.querySelector('.cc-number'),
exp = document.querySelector('.cc-exp'),
cvc = document.querySelector('.cc-cvc'),
validation = document.querySelector('.validation');
Payment.restrictNumeric(numeric);
Payment.formatCardNumber(number);
Payment.formatCardExpiry(exp);
Payment.formatCardCVC(cvc);
document.querySelector('form').onsubmit = function(e) {
e.preventDefault();
J.toggleClass(document.querySelectorAll('input'), 'invalid');
J.removeClass(validation, 'passed failed');
var cardType = Payment.fns.cardType(J.val(number));
J.toggleClass(number, 'invalid', !Payment.fns.validateCardNumber(J.val(number)));
J.toggleClass(exp, 'invalid', !Payment.fns.validateCardExpiry(Payment.cardExpiryVal(exp)));
J.toggleClass(cvc, 'invalid', !Payment.fns.validateCardCVC(J.val(cvc), cardType));
if (document.querySelectorAll('.invalid').length) {
J.addClass(validation, 'failed');
} else {
J.addClass(validation, 'passed');
}
}
</script>
</body>
</html>