Reputation: 61
I wanted to create a function where user key in a password and it has to match with this password (pwd1209!
). When user click on submit button
, it will check whether the typed password is matched with pwd1209!
.
<input type="password" id="pwd" class="form" placeholder="type your
password">
<button class="btn" id="submit"> Submit</button>
$(document).ready(function () {
$("#submit").click(function (e) {
if ($('#pwd').val() == '') {
alert('Key in a password.');
}
if ($('#pwd').val() != pwd1209!){
alert('Incorrect password.');
}
else{
alert('Correct password.');
}
});
Upvotes: 0
Views: 72
Reputation: 11
Your JS code is incomplete, apart of this, the key to your problem was that the password is a string, therefore it must be within quotes. For sure your solution is quite insecure as JS is visible so anyone can discover the password, you should try a backend solution to do the password check.
$(document).ready(function() {
$('#submit').click(function() {
if ($('#pwd').val() === '') {
alert('Add the password.');
} else if ($('#pwd').val() !== 'pwd1209!') {
alert('Incorrect password.');
} else {
alert('Correct password');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="password" id="pwd" class="form" placeholder="type your password">
<button class="btn" id="submit"> Submit</button>
Upvotes: 1