-
Notifications
You must be signed in to change notification settings - Fork 0
/
validateContactInput.js
90 lines (77 loc) · 2.61 KB
/
validateContactInput.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
var form = document.getElementById('contactForm');
var customerName = document.getElementById('customerName');
var email = document.getElementById('customerEmail');
var phone = document.getElementById('customerPhone');
var orderNumber = document.getElementById('orderNumber');
var message = document.getElementById('customerNote');
var buttonPressed = document.getElementById('submit');
if(buttonPressed.id == 'submit')
{
form.addEventListener( 'submit', (e) => {
var nameError = "";
var emailError = "";
var phoneError = "";
document.getElementById("nameError").innerHTML = "";
document.getElementById("emailError").innerHTML = "";
document.getElementById("phoneError").innerHTML = "";
var allErrors = [];
customerName.value = String(customerName.value).trim();
var regName = /^[a-zA-Z-' ]*$/;
if( !customerName.value.match(regName) )
{
nameError = "Only letters and white space allowed";
document.getElementById( "nameError" ).innerHTML = nameError;
allErrors.push( nameError );
}
email.value = String(email.value).trim();
var regEmail = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
if( !email.value.match(regEmail) )
{
emailError = "Invalid email format";
document.getElementById( "emailError" ).innerHTML = emailError;
allErrors.push( emailError );
}
if( phone.value != '' || phone.value != null )
{
phone.value = String(phone.value).trim();
regPhone = /^([0-9]{8}|[0-9]{7})*$/;
if( !phone.value.match( regPhone ) )
{
phoneError = "Enter a valid phone number";
document.getElementById( "phoneError" ).innerHTML = phoneError;
allErrors.push( phoneError );
}
}
if(allErrors.length > 0)
{
e.preventDefault();
document.getElementById( "sendError" ).innerHTML = "Message Not Sent!";
}
else
{
sendMessage();
document.getElementById( "sendError" ).innerHTML = "Message Sent!";
e.preventDefault();
}
})
function sendMessage() {
var var_str = "customerName=" + customerName.value + "&customerEmail=" + email.value + "&customerMessage=" + message.value;
if( !phone.value.length == 0)
{
var_str += "&customerPhone=" + phone.value;
}
if(!orderNumber.value.length == 0)
{
var_str += "&orderNumber=" + message.orderNumber;
}
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
document.getElementById("sendError").innerHTML = this.responseText;
}
};
xhr.open("POST", "validateContactInput.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(var_str);
}
}