-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (72 loc) · 1.89 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
const form = document.getElementById('form');
const username = document.getElementById('username');
const email = document.getElementById('email');
const password1 = document.getElementById('password1');
const password2 = document.getElementById('password2');
// Event Handler
form.addEventListener('submit',function(e){
e.preventDefault(); // data will not submit to server
checkInputs();
});
//Functions
function checkInputs(){
const usernameValue = username.value.trim();
const emailValue = email.value.trim();
const password1Value = password1.value.trim();
const password2Value = password2.value.trim();
if( usernameValue ==='')
{
showError(username,"Username can not be blank");
}
else
{
showSuccess(username);
}
if( emailValue ==='')
{
showError(email,"Email Id can not be blank");
}
else if(!isEmailValid(emailValue))
{
showError(email,"Email is not Valid");
}
else
{
showSuccess(email);
}
if( password1Value ==='')
{
showError(password1,"Password can not be blank");
}
else
{
showSuccess(password1);
}
if( password2Value ==='')
{
showError(password2,"Password can not be blank");
}
else if(password2Value != password1Value)
{
showError(password2,"Passwords not matched");
}
else
{
showSuccess(password2);
}
}
function showError(input,msg)
{
const formControl = input.parentNode;
formControl.className = 'form-control error';
const small = formControl.querySelector('small');
small.innerHTML = msg;
}
function showSuccess(input)
{
const formControl = input.parentNode;
formControl.className = 'form-control success';
}
function isEmailValid(email1){
return /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9]+)\.([a-zA-Z]{2,3})$/.test(email1); // email is the email value
}