BW Network
    Login | Register 

Stats
1 visitors online
Guests: 1
Registered: 0



SafeSurf Logo

Website BuildingTutorials → Javascript: Login Script (Part 5)

You can use this script on your own site and modify it as you wish!

<HTML>
<HEAD>
<TITLE>Password script
</TITLE>
<script>
//Passcheck script - www.benwiggy.com
function passcheck() {
//Set login success to bad to prevent refreshes of page affecting login
document.entranceform.login.value="bad"
//Define the username and password variables
username = document.entranceform.username.value.toLowerCase();
password = document.entranceform.password.value.toLowerCase();
//Check each username and password in turn
if (username=="benjamin" && password=="password1") {
document.entranceform.login.value="good";
}
if (username=="john" && password=="letmein") {
document.entranceform.login.value="good";
}
if (username == "matthew" && password=="iamdumb") {
document.entranceform.login.value="good";
}
//Sends them to pagepassed.html if successful
if (document.entranceform.login.value == "good") {
window.location="pagepassed.html";
}
//Gives them an error message if login is incorrect
if (document.entranceform.login.value != "good")
{
alert("Incorrect username and/or password");
}
}
</script>
</HEAD>
<body>
<form name="entranceform" onSubmit="passcheck()">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Submit!">
<input type="hidden" name="login">
</form>
</body>
</HTML>

The above script first defines a function. It then creates two variables, username and password and makes them lowercase to prevent capitals making a problem. It then checks the username to see if there is a matching password and if yes defines what happens (in this case moving to page 'pagepassed.html').

Congratulations on completing our tutorial, please check out the below links if you would like to learn more on Javascript!


  1. Introduction
  2. Part 1: Further Introduction
  3. Part 2: Basic Implementation
  4. Part 3: Strings & Variables
  5. Part 4: Forms
  6. Part 5: Login Script