forked from Rohit-k98/1.-Form-Validate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOldScript.js
More file actions
79 lines (66 loc) · 1.96 KB
/
OldScript.js
File metadata and controls
79 lines (66 loc) · 1.96 KB
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
// console.log(`Check`);
const form = document.querySelector(`#form`);
const username = document.querySelector(`#username`);
const email = document.querySelector(`#email`);
const password = document.querySelector(`#password`);
const password2 = document.querySelector(`#password2`);
//Functions
function showSuccess(input) {
const formControl = input.parentElement;
formControl.className = "form-control success";
}
function showError(input, message) {
const formControl = input.parentElement;
formControl.className = "form-control error";
const errorMsg = formControl.querySelector("small");
errorMsg.innerText = message;
}
function isValidEmail(email) {
let checkEmail = new RegExp('[a-z0-9]+@[a-z]+\.[a-z]{2,3}');
return checkEmail.test(email);
}
//EvenListener
form.addEventListener(`submit`, function (e) {
e.preventDefault();
// console.log(form);
// console.log(`submit`);
// console.log(username.value);
// console.log(email.value)
// console.log(password.value)
// console.log(password2.value)
//Username
if (username.value === ``) {
showError(username, `Please enter a username`);
}
else { showSuccess(username) }
//email
if (email.value === ``) {
showError(email, `Please enter an email`);
}
else if (isValidEmail(email.value)) {
showSuccess(email);
}
else
showError(email, `Invalid Email`);
//password
if (password.value === ``) {
showError(password, `Please enter a password`);
}
else {
showSuccess(password);
}
//Confirm Password
if (password2.value == `` & password == ``) {
showError(password2, `Confirm your password`);
}
else {
showSuccess(password2);
}
//Password Match
if (password.value === password2.value & password2 !== ``) {
showSuccess(password2, `password matched`);
}
else {
showError(password2, `Password Did not match`);
}
})