-
Notifications
You must be signed in to change notification settings - Fork 0
/
project4.js
100 lines (87 loc) · 2.85 KB
/
project4.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
console.log("This is project 4");
const name = document.getElementById('name');
const email = document.getElementById('email');
const phone = document.getElementById('phone');
let validEmail = false;
let validPhone = false;
let validUser = false;
$('#failure').hide();
$('#success').hide();
// console.log(name, email, phone);
name.addEventListener('blur', ()=>{
console.log("name is blurred");
// Validate name here
let regex = /^[a-zA-Z]([0-9a-zA-Z]){2,10}$/;
let str = name.value;
console.log(regex, str);
if(regex.test(str)){
console.log('Your name is valid');
name.classList.remove('is-invalid');
validUser = true;
}
else{
console.log('Your name is not valid');
name.classList.add('is-invalid');
validUser = false;
}
})
email.addEventListener('blur', ()=>{
console.log("email is blurred");
// Validate email here
let regex = /^([_\-\.0-9a-zA-Z]+)@([_\-\.0-9a-zA-Z]+)\.([a-zA-Z]){2,7}$/;
let str = email.value;
console.log(regex, str);
if(regex.test(str)){
console.log('Your email is valid');
email.classList.remove('is-invalid');
validEmail = true;
}
else{
console.log('Your email is not valid');
email.classList.add('is-invalid');
validEmail = false;
}
})
phone.addEventListener('blur', ()=>{
console.log("phone is blurred");
// Validate phone here
let regex = /^([0-9]){10}$/;
let str = phone.value;
console.log(regex, str);
if(regex.test(str)){
console.log('Your phone is valid');
phone.classList.remove('is-invalid');
validPhone = true;
}
else{
console.log('Your phone is not valid');
phone.classList.add('is-invalid');
validPhone = false;
}
})
let submit = document.getElementById('submit');
submit.addEventListener('click', (e)=>{
e.preventDefault();
console.log('You clicked on submit');
console.log(validEmail, validUser, validPhone);
// Submit your form here
if(validEmail && validUser && validPhone){
let failure = document.getElementById('failure');
console.log('Phone, email and user are valid. Submitting the form');
let success = document.getElementById('success');
success.classList.add('show');
// failure.classList.remove('show');
// $('#failure').alert('close');
$('#failure').hide();
$('#success').show();
}
else{
console.log('One of Phone, email or user are not valid. Hence not submitting the form. Please correct the errors and try again');
let failure = document.getElementById('failure');
failure.classList.add('show');
// success.classList.remove('show');
// $('#success').alert('hide');
$('#success').hide();
$('#failure').show();
}
})