-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
95 lines (85 loc) · 2.86 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
84
85
86
87
88
89
90
91
92
93
94
95
// Error msg
var errorFName = "First Name cannot be empty";
var errorLName = "Last Name cannot be empty";
var errorEmail = "Looks like this is not an email";
var errorPassword = "Password cannot be empty";
document.querySelectorAll('.form-control').forEach((item) => {
item.addEventListener("change", ()=> {
if (item.value != "") {
item.classList.add("active");
} else {
item.classList.remove("active");
}
});
});
const form = document.querySelector("form");
function validateForm() {
const fName = document.getElementById("fName");
const lName = document.getElementById("lName");
const emailId = document.getElementById("email");
const pass = document.getElementById("inputPassword2");
var firstName = fName.value;
var lastName = lName.value;
var email = emailId.value;
var password = pass.value;
const emailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
console.log(firstName);
console.log(lastName);
console.log(email);
console.log(password);
if (firstName === "") {
var errorText = document.getElementById("error-text-fName");
errorText.innerHTML = errorFName;
errorText.classList.add("active");
document.getElementById("fName").classList.add("icon-active");
document.getElementById("fName").classList.add("error");
}
if (lastName === "") {
var errorText = document.getElementById("error-text-lName");
errorText.innerHTML = errorLName;
errorText.classList.add("active");
document.getElementById("lName").classList.add("icon-active");
document.getElementById("lName").classList.add("error");
}
if (email === "" || !email.match(emailformat)) {
var errorText = document.getElementById("error-text-email");
errorText.innerHTML = errorEmail;
errorText.classList.add("active");
document.getElementById("email").classList.add("icon-active");
document.getElementById("email").classList.add("error");
}
if (password === "") {
var errorText = document.getElementById("error-text-password");
errorText.innerHTML = errorPassword;
errorText.classList.add("active");
document.getElementById("inputPassword2").classList.add("icon-active");
document.getElementById("inputPassword2").classList.add("error");
}
if (
firstName === "" ||
lastName === "" ||
email === "" ||
!email.match(mailformat) ||
password === ""
) {
return false;
}
return true;
}
const submitBtn = document.querySelector(".trial-button");
submitBtn.addEventListener("click", (obj) => {
obj.preventDefault();
var result = validateForm();
if (result) {
form.submit();
}
});
submitBtn.addEventListener("keydown", (obj) => {
obj.preventDefault();
if (obj.code === "Enter") {
var result = validateForm();
if (result) {
form.submit();
}
}
});