I am trying to check if two passwords (password1 and password2) in the input fields are the same. If I click the button there is automatically a text which is not in the corresponding condition of the button. I am trying to fix to display a text when – 1) Checking of empty password field. 2) minimum password length validation. 3) maximum length of password validation. https://jsfiddle.net/chrismontage/onh51g93/4/
JavaScript
x
63
63
1
function verifyPassword() {
2
var pw1 = document.getElementById("password1").value;
3
var pw2 = document.getElementById("password2").value;
4
5
// Get the modal
6
var modal = document.getElementById("myModal");
7
8
// Get the button that opens the modal
9
var btn1 = document.getElementById("myBtn");
10
11
var confirm = document.getElementById("confirm");
12
13
// Get the <span> element that closes the modal
14
var span = document.getElementsByClassName("close")[0];
15
16
17
btn1.onclick = function() {
18
modal.style.display = "block";
19
20
//check empty password field
21
22
if (pw1 == "") {
23
document.getElementById("message1").innerHTML = "*Please put your new password!";
24
return false;
25
}
26
27
//minimum password length validation
28
if (pw1.length < 8) {
29
document.getElementById("message1").innerHTML = "**Password length must be atleast 8 characters";
30
return false;
31
}
32
33
//maximum length of password validation
34
if (pw1.length > 15) {
35
document.getElementById("message1").innerHTML = "*Password length must not exceed 15 characters";
36
return false;
37
} else {
38
if (pw1 == pw2) {
39
document.getElementById("message1").innerHTML = "Passwords match!";
40
} else {
41
document.getElementById("message1").innerHTML = "Passwords not match!";
42
}
43
}
44
45
}
46
47
confirm.onclick = function() {
48
modal.style.display = "none";
49
}
50
51
// When the user clicks on <span> (x), close the modal
52
span.onclick = function() {
53
modal.style.display = "none";
54
}
55
56
// When the user clicks anywhere outside of the modal, close it
57
window.onclick = function(event) {
58
if (event.target == modal) {
59
modal.style.display = "none";
60
}
61
}
62
63
}
JavaScript
1
121
121
1
/* The Modal (background) */
2
3
.modal {
4
display: none;
5
/* Hidden by default */
6
position: fixed;
7
/* Stay in place */
8
z-index: 1;
9
/* Sit on top */
10
padding-top: 100px;
11
/* Location of the box */
12
left: 0;
13
top: 0;
14
width: 100%;
15
/* Full width */
16
height: 100%;
17
/* Full height */
18
overflow: auto;
19
/* Enable scroll if needed */
20
background-color: rgb(0, 0, 0);
21
/* Fallback color */
22
background-color: rgba(0, 0, 0, 0.4);
23
/* Black w/ opacity */
24
}
25
26
27
/* Modal Content */
28
29
.modal-content {
30
position: relative;
31
background-color: #fefefe;
32
margin: auto;
33
padding: 0;
34
border: 1px solid #888;
35
max-width: 40%;
36
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
37
-webkit-animation-name: animatetop;
38
-webkit-animation-duration: 0.4s;
39
animation-name: animatetop;
40
animation-duration: 0.4s
41
}
42
43
44
/* Add Animation */
45
46
@-webkit-keyframes animatetop {
47
from {
48
top: -300px;
49
opacity: 0
50
}
51
to {
52
top: 0;
53
opacity: 1
54
}
55
}
56
57
@keyframes animatetop {
58
from {
59
top: -300px;
60
opacity: 0
61
}
62
to {
63
top: 0;
64
opacity: 1
65
}
66
}
67
68
69
/* The Close Button */
70
71
.close {
72
color: white;
73
float: right;
74
font-size: 28px;
75
font-weight: bold;
76
}
77
78
.close:hover,
79
.close:focus {
80
color: $color5;
81
text-decoration: none;
82
cursor: pointer;
83
}
84
85
.modal-header {
86
background-color: $color1;
87
color: white;
88
text-align: center;
89
}
90
91
.modal-header h2 {
92
color: $color1;
93
text-align: center;
94
}
95
96
.modal-body {
97
padding: 2px 16px;
98
}
99
100
.modal-body h4 {
101
font-family: $font1;
102
font-weight: normal;
103
color: $color4;
104
font-size: 20px;
105
}
106
107
.modal-footer {
108
padding: 2px 16px;
109
color: white;
110
}
111
112
.modal-footer .btn {
113
background-color: $color1;
114
font-family: $font1;
115
font-weight: normal;
116
color: $color3;
117
}
118
119
.modal-footer .btn:hover {
120
color: $color5;
121
}
JavaScript
1
25
25
1
<label for="password">New Password</label>
2
<input type="password" class="input" id="password1">
3
<label for="cnfm-password">Confirm Pasword</label>
4
<input type="password" class="input" id="password2">
5
<input onclick="verifyPassword()" type="submit" value="Save Changes" class="btn" id="myBtn">
6
7
<!-- The Modal -->
8
<div id="myModal" class="modal">
9
10
<!-- Modal content -->
11
<div class="modal-content">
12
<div class="modal-header">
13
<img src="/images/logo.png" alt="SAMPLE" width="120" class="mx-auto">
14
<span class="close">×</span>
15
</div>
16
17
<div class="modal-body">
18
<h4 class="text-center"><span id="message1"></span></h4>
19
</div>
20
<div class="modal-footer mx-auto">
21
<button class="btn" id="confirm">Okay</button>
22
23
</div>
24
</div>
25
</div>
Advertisement
Answer
you have to fetch the password input value in the button click listener function.
JavaScript
1
66
66
1
function verifyPassword(){
2
var pw1 = document.getElementById("password1");
3
var pw2 = document.getElementById("password2");
4
5
// Get the modal
6
var modal = document.getElementById("myModal");
7
8
// Get the button that opens the modal
9
var btn1 = document.getElementById("myBtn");
10
11
var confirm = document.getElementById("confirm");
12
13
// Get the <span> element that closes the modal
14
var span = document.getElementsByClassName("close")[0];
15
16
17
btn1.onclick = function () {
18
19
modal.style.display = "block";
20
21
//check empty password field
22
23
if(pw1.value == "") {
24
document.getElementById("message1").innerHTML = "*Please put your new password!";
25
return false;
26
}
27
28
//minimum password length validation
29
if(pw1.value.length < 8) {
30
document.getElementById("message1").innerHTML = "**Password length must be atleast 8 characters";
31
return false;
32
}
33
34
//maximum length of password validation
35
if(pw1.value.length > 15) {
36
document.getElementById("message1").innerHTML = "*Password length must not exceed 15 characters";
37
return false;
38
} else {
39
if(pw1.value == pw2.value){
40
document.getElementById("message1").innerHTML= "Passwords match!";
41
}
42
43
else {
44
document.getElementById("message1").innerHTML= "Passwords not match!";
45
}
46
}
47
48
}
49
50
confirm.onclick = function () {
51
modal.style.display = "none";
52
}
53
54
// When the user clicks on <span> (x), close the modal
55
span.onclick = function() {
56
modal.style.display = "none";
57
}
58
59
// When the user clicks anywhere outside of the modal, close it
60
window.onclick = function(event) {
61
if (event.target == modal) {
62
modal.style.display = "none";
63
}
64
}
65
}
66