-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (45 loc) · 1.64 KB
/
index.js
File metadata and controls
55 lines (45 loc) · 1.64 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
document.addEventListener('DOMContentLoaded', () => {
// get form element
const formElement = document.querySelector('#form');
// add event for submit form
formElement.addEventListener('submit', (e) => {
e.preventDefault();
// email regex for check email
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
// make form data
const formData = new FormData(formElement);
// get data
const name = formData.get('user_name');
const email = formData.get('user_email');
const password = formData.get('user_password');
if (!name) {
alert('Name field is empty. Please fill it');
} else if (!email) {
alert('Email field is empty. Please fill it');
} else if (!emailRegex.test(email)) {
alert('Email is not valid');
} else if (!password) {
alert('Password field is empty. Please fill it');
} else if (password.length < 8) {
alert('Password minimal 8 character');
} else {
alert('Success');
alert(`Welcome ${name}`);
// clear form
formElement.reset();
}
});
togglePassword();
});
const togglePassword = () => {
// toggle password
const passwordInput = document.querySelector('#user_password');
const passwordToggle = document.querySelector('#toggle_password');
passwordToggle.addEventListener('click', () => {
if (passwordInput.type === 'password') {
passwordInput.type = 'text';
} else {
passwordInput.type = 'password';
}
});
}