-
Notifications
You must be signed in to change notification settings - Fork 21
/
validateNewsletterInput.js
49 lines (40 loc) · 1.46 KB
/
validateNewsletterInput.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
var newsletterForm = document.getElementById('newsletterForm');
var mail = document.getElementById('mail');
var buttonPressed = document.getElementById('subscribe');
if(buttonPressed.id == "subscribe")
{
newsletterForm.addEventListener('submit', (f) => {
var mailError = "";
document.getElementById("errorMessage").innerHTML = "";
var allErrors = [];
mail.value = String(mail.value).trim();
var regmail = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
if( !mail.value.match(regmail) )
{
mailError = "Invalid mail format";
document.getElementById("mailError").innerHTML = mailError;
allErrors.push(mailError);
}
if(allErrors.length > 0)
{
f.preventDefault();
}
else
{
sendToServer();
f.preventDefault();
}
})
function sendToServer() {
var var_str = "mail=" + mail.value;
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
document.getElementById("errorMessage").innerHTML = this.responseText;
}
};
xhr.open("POST", "validateNewsletterInput.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(var_str);
}
}