Very basic JavaScript password protection

This will show you how to make a password page. This should never be used for anything! Ever! It’s purely just to show you how it could be done. But it is literally less lecture than leaving your front door wide open.

In you you put in your password and if it is correct you go the another page. If you are incorrect you go to a wrong password page. Take a look at the script.

<script language="JavaScript">
function passwordOK(anystring) {
anystring = anystring.toUpperCase()

if (anystring == "GREEN" || anystring == "BLUE" || anystring
== "RED" || anystring == "YELLOW") {
/*Add more passwords if you'd like, but ALL PASSWORDS MUST BE IN CAPS!*/
alert('You got it right!')
alert('Now taking you to the hidden page.')
location="page2.htm"
//Change page2.htm to your hidden page
}


else {
alert ("Please enter the CORRECT password next time.")
location="wrongpage.htm"
/*substitute your own wrong page for wrongpage.htm*/
}
}
</script>
<form>
Password:
<input type="password" name="pass" Size="20" onChange="passwordOK(this.value)" />
</form>

As you can see the input box triggers the javascript function ‘passwordOK.’ Below the function name, in the next paragraph and what to do if the password is correct. Below that is the alert if the password is incorrect. Have a play around with the script and see what you can do. The whole thing can be copied in the body tag of your page.

Timeline

Newsletter

Don't have time to check my blog? Get a weekly email with all the new posts. This is my personal blog, so obviously it is 100% spam free.

Metadata

Tags: , ,

This entry was posted on Monday, December 30th, 2002 at 2:22 pm and is filed under Programming, Tech. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.