-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.js
115 lines (100 loc) · 3.07 KB
/
form.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
// Initializing the emailjs application.
(function(){
emailjs.init("user_3KGBY3C7G3MeNxiVEvNCj");
})();
// Field border turns red in case of invalid input.
function formError(field,id){
document.getElementById(id).style.borderColor="red";
}
//}
//Rest All the fields
function resetAll(arr){
for(i=0;i<arr.length;i++){
document.getElementById(arr[i]).value="";
document.getElementById(arr[i]).style.borderColor="";
}
}
// Checks for empty fields.
function doIt(){
var state=true;
function check_null(value,id,field){
if(value){
return;
}
else{
state=false;
formError(field,id);
return 1;
}
}
// Checks for invalid characters or entries and send a message to the user accordingly.
function check_typing(value,id,field,reg){
if(check_null(value,id,field)==1){
formError(field,id);
alert("Couldn't submit because the field "+field+ " is empty.");
return 1;
}
else{
if(reg.test(value)){
return 0;
}
else{
state=false;
formError(field,id);
alert("Couldn't submit because the " +field+ " that you provided is not valid or contains unexpected characters.");
return 2;
}
}
}
arr=[document.getElementById("fname").value,
document.getElementById('lname').value,
document.getElementById('email').value,
document.getElementById('phone').value,
document.getElementById('message').value];
for(i=0;i<arr.length;i++){
if(!state){
break;
}
switch(i){
case 0:
check_null(arr[0],"fname","First Name");
check_typing(arr[0],"fname","First Name",/^[a-z ,.'-]+$/i);
break;
case 1:
check_null(arr[1],"lname","Last Name");
check_typing(arr[1],"lname","Last Name",/^[a-z ,.'-]+$/i);
break;
case 2:
check_null(arr[2],"email","Email");
check_typing(arr[2],"email","Email",/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
break;
case 3:
check_null(arr[3],"phone","Phone");
check_typing(arr[3],"phone","Phone",/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/);
break;
case 4:
check_null(arr[4],"message","Message");
check_typing(arr[4],"message","Message",/(.*[a-z]){1}/i);
break;
}
}
// If all fields are appropriately filled, send a success message to the user and send the email.
if(state){
emailjs.send("gmail","contact",{
message: document.getElementById('message').value,
fname: document.getElementById("fname").value,
lname: document.getElementById('lname').value,
email: document.getElementById('email').value,
phone: document.getElementById('phone').value
})
.then(
function(response) {
console.log("SUCCESS", response);
resetAll(['fname','lname','email','phone','message']);
alert("Thank you for your interest in GHM Robotics. Your query has been submitted, we will return to you shortly.");
},
function(error) {
console.log("FAILED", error);
}
);};
}