'How to allow unlimited password attempts in javascript
var password;
var pass1="administrator";
password=prompt('Please enter password',' ');
if (password!=pass1)
alert('password is not currect');
else
{
window.location="home.html";
}
I want to make the number of attempts on the password unlimited
Solution 1:[1]
remove the else part from your code.
var password;
var pass1="administrator";
password = prompt('Please enter password',' ');
if (password!=pass1)
alert('password is not currect');
Solution 2:[2]
Use this code and add breaking statement as you wish to break the loop:
while (true) {
var password;
var pass1 = "administrator";
password = prompt('Please enter password', ' ');
if (password === null) break;
if (password != pass1)
alert('password is not currect');
else {
window.location = "home.html";
break;
}
}
Solution 3:[3]
I understand what you want to do, you can use another modal for asking for passwords.
else
if you want to continue with the javascript prompt method then you can reload the page while entering the wrong password.
I have added location.reload(); when condition is false.
<script>
var password;
var pass1="administrator";
password=prompt('Please enter password','');
if (password!=pass1){
alert('password is not currect');
location.reload();// reload your page in false condtion.
}
else
{
window.location="home.html";
}
</script>
Solution 4:[4]
var password=prompt('Please enter password',' ');
var pass1="administrator";
// executes until password==pass1
while (password!=pass1) {
alert('password is not correct');
password=prompt('Please enter password',' ');
}
window.location="home.html";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muhammad |
| Solution 2 | mplungjan |
| Solution 3 | Shubham Raturi |
| Solution 4 | YoyoO |
