How can I lock the website using prompt() command of js ? | Sololearn: Learn to code for FREE!

+1

How can I lock the website using prompt() command of js ?

I want to lock my website that if visitor enters wrong password in prompt command then, it will ask again and again for correct password until visitor enters correct one. How can I do ? See the code below :- let confirm = prompt("Enter passcode to view site"); switch (confirm) { case "123": alert("Welcome to website"); break; default: alert("Try again"); confirm = prompt("Enter passcode to view site"); break; }

1/24/2022 2:20:23 PM

ℋℜҜ

3 Answers

New Answer

+2

Use a looping... let valid=false; do{ let confirm = prompt("Enter passcode to view site"); switch (confirm) { case "123": alert("Welcome to website"); valid=true; break; default: alert("Try again"); valid=false; break; } }while(!valid);

+1

Jayakrishna🇮🇳 Thanks bro very much. One more question is there any solution to remove the alert of "Try again!" after some seconds i.e. after 5 seconds alert box of "Try again!" will disappear.

0

I don't think. Alert makes background program to stop until you press ok. So time setting won't run on alert. But you can alternatives, of making custum alert through a function or div element and set timeout.. https://stackoverflow.com/questions/15466802/how-can-i-auto-hide-alert-box-after-it-showing-it https://stackoverflow.com/questions/1962861/javascript-alert-box-with-timer