-
Notifications
You must be signed in to change notification settings - Fork 0
/
EX_oop.js
56 lines (47 loc) · 1.46 KB
/
EX_oop.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
class Validator {
static REQUIRED = 'REQUIRED';
static MIN_LENGTH = 'MIN_LENGTH';
static validate(value, flag, validatorValue) {
if (flag === this.REQUIRED) {
return value.trim().length > 0;
}
if (flag === this.MIN_LENGTH) {
return value.trim().length > validatorValue;
}
}
}
class User {
constructor(uName, uPassword) {
this.userName = uName;
this.password = uPassword;
}
greet() {
console.log('Hi, I am ' + this.userName);
}
}
class UserInputForm {
constructor() {
this.form = document.getElementById('user-input');
this.userNameInput = document.getElementById('username');
this.passwordInput = document.getElementById('password');
this.form.addEventListener('submit', this.signupHandler.bind(this));
}
signupHandler(event) {
event.preventDefault();
const enteredUserName = this.userNameInput.value;
const enteredPassword = this.passwordInput.value;
if (
!Validator.validate(enteredUserName, Validator.REQUIRED) ||
!Validator.validate(enteredPassword, Validator.MIN_LENGTH, 5)
) {
alert(
'Invalid input - username or password is wrong (password should be at least six characters).'
);
return;
}
const newUser = new User(enteredUserName, enteredPassword);
console.log(newUser);
newUser.greet();
}
}
new UserInputForm();