-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.js
More file actions
54 lines (35 loc) · 1.02 KB
/
validator.js
File metadata and controls
54 lines (35 loc) · 1.02 KB
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
const validator = {
isValid: function (creditCardNumber) {
const array = creditCardNumber.split("").map(Number);
//console.log(array);
const reversed = array.reverse();
let sum = 0;
for (let i = 0; i < reversed.length; i++) {
const pos = i + 1;
if (pos % 2 === 0) {
reversed[i] = reversed[i] * 2;
}
if (reversed[i] > 9) {
const num = reversed[i].toString().split("").map(Number);
//console.log(num);
reversed[i] = num[0] + num[1];
}
sum += reversed[i]
}
return sum % 10 === 0;
},
maskify: (numero) => {
const fourSaved = numero.slice(-4)
//console.log(fourSaved)
let michi = ""
for (let i = 0; i < numero.length; i++) {
michi = michi + "#"
}
//console.log(michi)
const formichi = michi.slice(0, michi.length - 4)
const result = formichi + fourSaved
//console.log(result)
return result
}
}
export default validator;