/    Sign up×
Community /Pin to ProfileBookmark

Need help with code for simple password field with redirect pages

Hi all,

I’m obviously not an expert. Can anyone please help me with html code to make a simple password field? I need it to redirect to one page if the password is correct and also redirect to a different page if the password is incorrect.

It doesn’t have to be a very secure login. It’s for a simple website form to submit an answer that is either right or wrong.

The following code is what I’ve been playing with and the redirect for the false password doesn’t work. Also, it’s a button with a popup prompt and I would actually prefer a text field to submit on keying enter.

If anyone can help me with either problem, I would so appreciate any help.

<html>
<HEAD>
<SCRIPT LANGUAGE=”JavaScript”>
function LogIn(){
password=”mypassword”;
password=prompt(“Enter password:”,””);
password=password.toLowerCase();
if (password==”mypassword”) {
loggedin=true;
window.location=”http://MyURLredirectRightPWD.html“;
}
if (password==false) {
loggedin=false;
window.location=”http://MyURLredirectWrongPWD.html“;
}
}
</SCRIPT>
<BODY>
<center>
<form><input type=button value=”Enter Password” onClick=”LogIn()”></form>
</center>
</body>
</html>

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@shakazorbaJan 08.2017 — [CODE]

<html>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function LogIn(){
password="mypassword";
password=prompt("Enter password:","");
password=password.toLowerCase();
if (password=="mypassword") {
loggedin=true;
window.location="http://msn.com";
}
[COLOR="#FF0000"]if (password != "mypassword") {[/COLOR]
loggedin=false;
window.location="http://cnn.com";
}
}
</SCRIPT>
<BODY>
<center>
<form><input type=button value="Enter Password" onClick="LogIn()"></form>
</center>
</body>
</html>

[/CODE]
Copy linkTweet thisAlerts:
@Nicole17authorJan 08.2017 — Shakazorba,

Thank you very much! ?
Copy linkTweet thisAlerts:
@rootJan 09.2017 — Please use forum BBCode tags to wrap any code you post in your questions.

Also remember a few things you need, the document type and that HTML tags are in pairs and set out in the format of...
[code=html]<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
</script>
</head>
<body>
</body>
</html>[/code]



[code=html]<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<script>
function LogIn( fm ){
var pw = fm.password.value;
switch( pw ){
case "letmein":
gotoPlace = "http://google.com";
break;

default:
gotoPlace = "http://msn.com";
}
fm.action = gotoPlace;
}
</script>
</head>
<BODY>
<center>
<form name="loginpage" action="javascript:; method="post" onsubmit="return LogIn( this );">
<input name="password" type="password" value="" placeholder="Password">
<input name="Submit" type="submit" value="Login"></form>
</center>
</body>
</html>[/code]


One way is to use the form element and an HTML5 password field, as for validation of passwords using JavaScript, it is not wise in a production to use anything but a server side validation, for understanding forms and fields, perfectly fine.
Copy linkTweet thisAlerts:
@rootJan 09.2017 — Forum is doing that error thing again with trying to edit posts, noticed a typo...

[B]action="javascript:; [/B] should be [B]action="javascript:;"[/B]
Copy linkTweet thisAlerts:
@lon124Jan 16.2017 — Hi Nicole17,

just wanted to give you an alternative using an if...else... statement, seems a better fit to me under the circumstances. As always there are lots of ways to skin the cat...

[CODE]
password=password.toLowerCase();
if (password=="mypassword") {
loggedin=true;
window.location="http://MyURLredirectRightPWD.html";
}else{
loggedin=false;
window.location="http://MyURLredirectWrongPWD.html";
}
[/CODE]


As you're probably aware the 'loggedin' variable isn't going to be accessible anymore once you're redirected. And this is far from a safe solution for production environments.

Happy coding + happy to answer further questions.
Copy linkTweet thisAlerts:
@rootJan 16.2017 — Hi Nicole17,

just wanted to give you an alternative using an if...else... statement, seems a better fit to me under the circumstances. As always there are lots of ways to skin the cat...

[CODE]
password=password.toLowerCase();
if (password=="mypassword") {
loggedin=true;
window.location="http://MyURLredirectRightPWD.html";
}else{
loggedin=false;
window.location="http://MyURLredirectWrongPWD.html";
}
[/CODE]


As you're probably aware the 'loggedin' variable isn't going to be accessible anymore once you're redirected. And this is far from a safe solution for production environments.

Happy coding + happy to answer further questions.[/QUOTE]


[B]window.location[/B] is now [B]location.replace( "http://whereToGoTo.com" )[/B]
Copy linkTweet thisAlerts:
@SergeantNeptuneJan 10.2022 — Hello,

I do have a question. Is it possible to have multiple passwords to be redirected to another URL? How can I include 2 passwords for example I want to use 'password1' or 'password2' after entering either one of the correct passwords I will be redirected to another URL. Thank you!
Copy linkTweet thisAlerts:
@NogDogJan 10.2022 — > @SergeantNeptune#1641324 multiple passwords to be redirected to another URL

Maybe use a switch block? https://www.w3schools.com/js/js_switch.asp 🤷
×

Success!

Help @Nicole17 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...