-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodalForm.js
More file actions
84 lines (70 loc) · 2.21 KB
/
Copy pathmodalForm.js
File metadata and controls
84 lines (70 loc) · 2.21 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
73
74
75
76
77
78
79
80
81
82
83
84
const modalForm = $(".modal-window .specify-info__form"),
checkEmail = $(".modal-window .check-email");
const userName = modalForm.find(".name"),
userEmail = modalForm.find(".email"),
userPhone = modalForm.find(".phone");
userName.on("keypress", function(event) {
const verified = String.fromCharCode(event.which).match(/\w/);
if (verified) {
event.preventDefault();
}
});
userPhone.intlTelInput( {
onlyCountries: ["ru","gb","ua"],
preferredCountries: [ "ua"],
utilsScript: "sources/intTelInput/utils.js"
});
modalForm.on("submit", function (event) {
event.preventDefault();
const fields = {
name: userName.val(),
email: userEmail.val(),
phone: userPhone.val()
};
if (validateUser(fields)) {
console.log(JSON.stringify(fields));
toNextStep();
}
});
checkEmail.find(".confirm").on("click", function (event) {
event.preventDefault();
$.modal.close();
});
const toNextStep = () => {
$(".modal-window .specify-info").css("display", "none");
$(".modal-window .check-email").css("display", "flex");
};
const validateUser = fields => {
const result = [];
const items = {
name: {
isValid(name) {
return name.length > 2
},
selector: ".specify-info__form .name"
},
email: {
isValid(email) {
return /^([a-z0-9_-]+\.)*[a-z0-9_-]+@[a-z0-9_-]+(\.[a-z0-9_-]+)*\.[a-z]{2,6}$/.test(email);
},
selector: ".specify-info__form .email"
},
phone: {
isValid(phone) {
return /^\d{10,}$/.test(phone)
},
selector: ".specify-info__form .phone"
}
};
for (let key in fields) {
if (items.hasOwnProperty(key)) {
if (items[key].isValid(fields[key])) {
$(items[key].selector).closest(".form-item").removeClass("invalid")
} else {
result.push(key);
$(items[key].selector).closest(".form-item").addClass("invalid");
}
}
}
return !result.length;
};