-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (104 loc) · 3.69 KB
/
index.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function MyForm() {}
MyForm.prototype.validate = function() {
var fioRE = /^[а-яА-ЯёЁa-zA-Z]{2,40}\s+[а-яА-ЯёЁa-zA-Z]{2,40}\s+[а-яА-ЯёЁa-zA-Z]{2,40}\s*$/;
var emailRE = /^([a-z0-9_\.-]+)@(?=(ya.ru|yandex.com|yandex.ru|yandex.by|yandex.ua|yandex.kz))\2/;
var phoneRE = /\+7\(\d{3}\)\d{3}-\d{2}-\d{2}/;
var elems = document.forms["myForm"].elements;
var isValid;
var errorFields = [];
if (fioRE.test(elems[0].value)) {
elems[0].classList.remove("error");
} else {
elems[0].classList.add("error");
errorFields.push(elems[0].name);
}
if (emailRE.test(elems[1].value)) {
elems[1].classList.remove("error");
} else {
elems[1].classList.add("error");
errorFields.push(elems[1].name);
}
if (phoneRE.test(elems[2].value)) {
var sum = 0;
var digits = elems[2].value.match(/\d+/g).join("");
for (var i = 0; i < digits.length; i++) {
sum += parseInt(digits[i], 10);
}
console.log(sum);
if (sum < 31) {
elems[2].classList.remove("error");
} else {
elems[2].classList.add("error");
errorFields.push(elems[2].name);
}
} else {
elems[2].classList.add("error");
errorFields.push(elems[2].name);
}
//console.log({ isValid: !errorFields.length > 0, errorFields });
return { isValid: !errorFields.length > 0, errorFields };
};
MyForm.prototype.getData = function() {
var elems = document.forms["myForm"].elements;
var fio = elems[0].value;
var email = elems[1].value;
var phone = elems[2].value;
return { fio: fio, email: email, phone: phone };
};
MyForm.prototype.setData = function(objForm) {
let inputs = document.querySelectorAll("input[type=text]");
for (let j = 0; j < inputs.length; j++) {
if (objForm.hasOwnProperty(inputs[j].name)) {
inputs[j].value = objForm[inputs[j].name];
}
}
};
MyForm.prototype.submit = function() {
if (!MyForm.prototype.validate().isValid) return;
var xhr = new XMLHttpRequest();
// [{"status":"success"}]
// [{"status":"error","reason":"string"}]
// [{"status":"progress","timeout":2000}]
xhr.open("GET", document.forms["myForm"].action, false);
//xhr.open('GET','data:text/javascript;charset=utf-8,[{"status":"progress","timeout":2000}]',true)
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// Track the state changes of the request.
xhr.onreadystatechange = function() {
var DONE = 4; // readyState 4 means the request is done.
var OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
console.log(xhr.responseText);
var status = JSON.parse(xhr.responseText);
var resultContainer = document.getElementById("resultContainer");
resultContainer.className = status[0]["status"];
switch (status[0]["status"]) {
case "success":
resultContainer.innerText = "Success";
break;
case "error":
resultContainer.innerText = JSON.parse(xhr.responseText)[0].reason;
break;
case "progress":
var time = JSON.parse(xhr.responseText)[0].timeout;
resultContainer.innerText = time.toString();
setTimeout(MyForm.prototype.submit, time);
break;
}
/* status.forEach(function (name) {
console.log(name.status);
}); */
} else {
console.log("Error: " + xhr.status); // An error occurred during the request.
}
}
};
// Send the request to send-ajax-data.php
//xhr.send(null);
xhr.send();
};
document.forms["myForm"].addEventListener("submit", function(event) {
event.preventDefault();
MyForm.prototype.submit();
return false;
});