-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
87 lines (69 loc) · 2.47 KB
/
script.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
const nameError = document.getElementById("nameError");
const emailError = document.getElementById("emailError");
const phoneError = document.getElementById("phoneError");
const messageError = document.getElementById("messageError");
const submitError = document.getElementById("submitError");
function validateName(){
const name = document.getElementById("name").value;
if(name.length == 0){
nameError.innerHTML = "Full name is required!";
return false;
}
if(!name.match(/^[A-Za-z]+(?:\s{1}[A-Za-z]+){1,2}$/)){
nameError.innerHTML = "Your full name is required!";
return false;
}
nameError.innerHTML = "<img src='images/valid.png'>" + " <p>Perfect!</p>";
return true;
}
function validateEmail(){
const email = document.getElementById("email").value;
if(email.length == 0){
emailError.innerHTML = "Email address is required!";
return false;
}
if(!email.match(/^[A-Za-z0-9]+(?:\.[A-Za-z0-9]+)*@[A-Za-z]+[.][A-Za-z]{2,5}(?:\.[A-Za-z]{2})?$/)){
emailError.innerHTML = "Enter your valid email address!";
return false;
}
emailError.innerHTML = "<img src='images/valid.png'>" + " <p>Perfect!</p>";
return true;
}
function validatePhone(){
const phone = document.getElementById("phone").value;
if(phone.length == 0){
phoneError.innerHTML = "Phone number is required!";
return false;
}
if(!phone.match(/^[0-9]{10}$/)){
phoneError.innerHTML = "Phone number should be 10 digits only!";
return false;
}
phoneError.innerHTML = "<img src='images/valid.png'>" + " <p>Perfect!</p>";
return true;
}
function validateMessage(){
const message = document.getElementById("message").value;
const minChars = 30;
const charLeft = minChars - message.length;
if(charLeft > 0){
messageError.innerHTML = "Min. " + charLeft + " characters are needed!"
return false;
}
if(message.length > 300){
messageError.innerHTML = "Max. 300 characters!";
return false;
}
messageError.innerHTML = "<img src='images/valid.png'>" + " <p>Perfect!</p>";
return true;
}
function validateForm(){
if(!validateName() || !validateEmail() || !validatePhone() || !validateMessage()){
submitError.style.display = "block";
submitError.innerHTML = "Please fix all errors first!";
setTimeout( () => {
submitError.style.display = "none";
}, 5000)
return false;
}
}