-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.js
More file actions
72 lines (63 loc) · 4.07 KB
/
validate.js
File metadata and controls
72 lines (63 loc) · 4.07 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const check_dni = (dni) => {
const validChars = 'TRWAGMYFPDXBNJZSQVHLCKET'
const nifRexp = /^[0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKET]{1}$/i
const nieRexp = /^[XYZ]{1}[0-9]{7}[TRWAGMYFPDXBNJZSQVHLCKET]{1}$/i
const str = dni.toString().toUpperCase()
if(!nifRexp.test(str) && !nieRexp.test(str)){/* Invalid Dni */}
const nie = str
.replace(/^[X]/, '0')
.replace(/^[Y]/, '1')
.replace(/^[Z]/, '2')
const letter = str.substr(-1)
const charIndex = parseInt(nie.substr(0, 8)) % 23
if(validChars.charAt(charIndex) === letter){/* Valid Dni */}
else {/* Invalid Dni */}
}
const check_email = (email) => {
const emailRegex = /^(([^<>()\[\]\\.,:\s@"]+(\.[^<>()\[\]\\.,:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if(emailRegex.test(email)){/* Valid Email */}
else {/* Invalid Email */}
}
const check_iban = (iban) => {
const ibanRegex = /([A-Z]{2})\s*\t*(\d\d)\s*\t*(\d\d\d\d)\s*\t*(\d\d\d\d)\s*\t*(\d\d)\s*\t*(\d\d\d\d\d\d\d\d\d\d)/g
if(ibanRegex.test(iban)){/* Valid Iban */}
else {/* Inalid Iban */}
}
const check_password_complex = (password) => {
//Should have 1 lowercase letter, 1 uppercase letter, 1 number, 1 special character and be at least 8 characters long
const passwordRegex = /(?=(.*[0-9]))(?=.*[\!@#$%^&*()\\[\]{}\-_+=~`|:"'<>,./?])(?=.*[a-z])(?=(.*[A-Z]))(?=(.*)).{8,}/
if(passwordRegex.test(password)){/* Valid Password */}
else {/* Invalid Password */}
}
const check_password_moderate = (password) => {
//Should have 1 lowercase letter, 1 uppercase letter, 1 number, and be at least 8 characters long
const passwordRegex = /(?=(.*[0-9]))((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.{8,}$/
if(passwordRegex.test(password)){/* Valid Password */}
else{/* Invalid Password */}
}
const check_username = (username) => {
//Alphanumeric string that may include _ and – having a length of 3 to 16 characters –
const usernameRegex = /^[a-z0-9_-]{3,16}$/
if(usernameRegex.test(username)){/* Valid Username */}
else {/* Invalid Username */}
}
const check_url = (url) => {
const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#()?&//=]*)/
if(urlRegex.test(url)){/* Valid Url */}
else{/* Invalid Url */}
}
const validateIP = (ip) => {
const ipRegex = /((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/g
if(ipRegex.test(ip)){/* Valid Ip */}
else{/* Invalid Ip */}
}
const check_hex = (hexadecimal) => {
const hexadecimalRegex = /^#?([a-f0-9]{6}|[a-f0-9]{3})$/
if(hexadecimalRegex.test(hexadecimal)){/* Valid Hex */}
else{/* Invalid hex */}
}
const check_cc = (card) => {
const creditCardRegex = /^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/
if(creditCardRegex.test(card)){/* Valid CC */}
else{/* Invalid CC */}
}