-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (65 loc) · 2.08 KB
/
index.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
const username = document.getElementById('usernameInput');
const email = document.getElementById('emailInput');
const password = document.getElementById('passwordInput');
const password2 = document.getElementById('password2Input');
const form = document.getElementById('form');
const setError = (element, message)=>{
const inputControl = element.parentElement;
const errorDisplay =inputControl.querySelector('.error');
errorDisplay.innerText = message;
inputControl.classList.add('error');
inputControl.classList.remove('success');
}
const setSuccess = (element)=>{
const inputControl = element.parentElement;
const errorDisplay =inputControl.querySelector('.error');
inputControl.classList.add('success');
inputControl.classList.remove('error')
errorDisplay.innerText = ''
}
form.addEventListener('submit', (event) =>{
event.preventDefault(); // dont refresh page
if (validateInputs()){
window.location.href = 'welcome.html'
}
})
const validateInputs =() => {
let usernameVal = username.value.trim();
let emailVal = email.value.trim();
let passwordVal = password.value.trim();
let password2Val = password2.value.trim();
let pass = false;
if (usernameVal === '' || usernameVal == null){
setError(username, 'Name is required')
pass = false;
}else{
setSuccess(username);
pass = true;
}
if (emailVal === '' ||emailVal == null){
setError(email,'Email required')
pass = false;
}else{
setSuccess(email);
pass = true;
}
if (passwordVal === ''|| passwordVal == null){
setError(password,'You did not enter a password')
pass = false;
}else if(passwordVal.length < 8){
setError(password, 'Password Length should be >8')
pass = false;
}
else{
setSuccess(password);
pass = true;
}
if (password2Val !==passwordVal){
setError( password2,'Your passwords dont match')
pass = false;
}else{
setSuccess(password2);
pass = true;
}
return pass;
}