How can I lock the website using prompt() command of js ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

24th Jan 2022, 2:20 PM
Harsh
Harsh - avatar
3 Answers
+ 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);
24th Jan 2022, 3:13 PM
Jayakrishna 🇮🇳
+ 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.
25th Jan 2022, 4:05 AM
Harsh
Harsh - avatar
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
25th Jan 2022, 10:31 AM
Jayakrishna 🇮🇳