-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
51 lines (44 loc) · 1.25 KB
/
main.js
File metadata and controls
51 lines (44 loc) · 1.25 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
const list = zvdata.countries.map(c => c.name);
const substringMatcher = (strs) => {
return function findMatches(q, cb) {
const matches = [];
substrRegex = new RegExp(q, 'i');
$.each(strs, function (i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
$('.sign-up__form .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'countries',
source: substringMatcher(list)
});
const post = document.querySelector('.sign-up__form #zip')
const validateZip = () => {
const country = document.querySelector('#kraina').value;
if (country) {
const code = (zvdata.countries.find(c => c.name === country))['alpha-2'];
if (code) {
const regexp = zvdata.regexp[code];
if (regexp) {
console.log('regexp', regexp);
const r = new RegExp(regexp);
if (r.test(post.value.toUpperCase()) || post.value === '') {
post.setCustomValidity('');
} else {
post.setCustomValidity(`Not a valid ${code} zip code.`);
}
}
} else {
post.setCustomValidity('');
}
}
};
document.querySelector('#kraina').addEventListener('change', validateZip);
post.addEventListener('input', validateZip);