TurtleProgrammer
TurtleProgrammer

Reputation: 11

JavaScript Username/Password Code Does Not Work

I am trying to write a website code for something and make a username and password. I do't know if this is completely logical or if the code would work even if it was fixed, but I am a beginner and I am working from nothing. Here is what I have:

<script>
alert("Welcome. Please click ok and enter your username.")
</script>
<script>
var userName=prompt("Please enter your username:","Username")
if(userName="person") {
	alert("Next, please enter your password")
}
else{
 	reload();
}
</script>
<h1><script>
var passWord=prompt("Please enter your password:","Password")
if(passWord="person") {
	document.write("Welcome to headquarters, Person!")
}
else{
	reload();
}
</script></h1>

I am not sure how to make it work, because as of now, it always goes to the "welcome to headquarters" part. It never uses else, and it uses every username and password as right. I hope this makes sense and is possible. I would like to stick to this code as much as possible in the end since I wrote it from things I knew, not from a website. Thanks! :)

Upvotes: 0

Views: 81

Answers (1)

Jonathan Levine
Jonathan Levine

Reputation: 86

When comparing the password you had only one '='. This assigns a value. To compare, you need two ('==').

<script>
alert("Welcome. Please click ok and enter your username.")
</script>
<script>
var userName=prompt("Please enter your username:","Username")
if(userName=="person") {
	alert("Next, please enter your password")
}
else{
 	reload();
}
</script>
<h1><script>
var passWord=prompt("Please enter your password:","Password")
if(passWord=="person") {
	document.write("Welcome to headquarters, Person!")
}
else{
	reload();
}
</script></h1>

By the way, NEVER use this for production. Anyone can view the source and see the password in plain text.

Upvotes: 2

Related Questions