0

I am trying to make a website that validates a specific password and if so it takes the user into my website I can't resolve the bug so can I get some help...?

<script language="Javascript">
  function check(x){
    if (x == "HI"){
      alert("Just Press Ok to Continue...");
    } else {
      alert("Nope... not gonna happen");
    }
  }

  var grape = alert("Just press Ok to Countinue...")

  function alertIt() {
    if (grape === true){
      window.location="http://www.google.com";
    } else {
      alert("Nope... not gonna happen")
    }
  }

}
</script>

<center>
<br /><h3>Enter the Password</h3>
<br />
<b>Password:</b>
<input type="password" id="pass" value="">
<br /><br />
<button onclick="javascript:check(document.getElementById('pass').value)">Check Password</button>
<button onclick="javascript:alertIt">On To my Website</button>

Thank you I am just a kid and still rough on my javascript and I didnt wanna use PHP.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • 2
    "I can't resolve the bug" ... **What** bug?? – Claies Dec 28 '16 at 17:43
  • 3
    You have an extra `}` just before your `` tag. Also, don't use inline HTML attributes to wire up events. They cause global anonymous wrapper functions to be created, they create spaghetti code and they don't follow W3C DOM Event standards. – Scott Marcus Dec 28 '16 at 17:45
  • 1
    @Shanimal You can't replace the `document.get.....` with `this` in this case because `this` will be the `button`, not the password box that the OP wants. – Scott Marcus Dec 28 '16 at 17:53
  • @ScottMarcus of course. lol – Shanimal Dec 28 '16 at 18:03
  • Possible duplicate of [What characters are valid for JavaScript variable names?](http://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names) – Auston Shafer Dec 29 '16 at 17:02

3 Answers3

1

grape will be equal to undefined.

This is because alert() always returns undefined (has no return value).

You want to use confirm() instead.

Kyle Baker
  • 3,424
  • 2
  • 23
  • 33
1

The main issue with your code is that you have two functions to accomplish one thing and your second function (alertIt) was based on a condition that was never going to be true.

Let's take the problems one at a time....

  1. First, you have an extra } just before your </script> tag. This would result in none of your code even executing. Let's hope that was just a copy/paste error.
  2. Next, this line: var grape = alert("Just press Ok to Countinue...") will always result in grape being undefined because an alert() never returns a value. That being the case, you can't expect: if (grape === true){ in your alertIt function to ever be true.
  3. Probably the most significant issue is that your code is overly complicated for the task you've stated you want to accomplish "validates a specific password and if so it takes the user into my website". To do this, you really just need the check function. You can get rid of the alertIt function and the "on to my website" button.
  4. Next (and this isn't the reason your code didn't work, but it is a suggestion for best-practices), don't use inline HTML attributes to wire up events (onclick, onmouseover, onmouseout, etc). They cause global anonymous wrapper functions to be created, they create spaghetti code that makes scaling and debugging code harder and they don't follow W3C DOM Event standards.
  5. Finally (and, again this is just to improve your code), styling should be done with CSS and not using deprecated HTML (i.e. <center>).

So, you're adjusted working code now looks like this. Please read the comments within the code for explanations of what is being done and why:

// When the DOM is fully loaded:
window.addEventListener("DOMContentLoaded", function(){

  // Get references to the elements that you'll need
  var password = document.getElementById("pass");
  var btnPassword = document.getElementById("btnCheck");

  // Then, instead of wiring HTML elements to JavaScript event callbacks in HTML
  // do it with the W3C DOM Event standard of: .addEventListener()
  btnPassword.addEventListener("click", function(){
    // Call your check function and pass it the password
    check(password.value);
  });

  // This is the only function you need. It combines the old "alertIt" with "check"  
  function check(x){
    if (x == "HI"){
      // If the password is correct, tell the user and proceed
      alert("Just Press Ok to Continue...");
      window.location="http://www.google.com";
    } else {
      // If not, just tell the user:
      alert("Nope... not gonna happen");
    }
  }
  
});
/* Write style rules to affect the parts of the document you want */
body { text-align:center; }
#pwd { font-weight:bold; }
<!-- Note that <center> and <b> have been moved into the CSS
     because styling should not be done in HTML. Also, note that
     <div> and <p> were added for structure and excessive <br> were
     removed. Again, don't use HTML for formatting.                  -->
<div>
  <h3>Enter the Password</h3>
  <br>
  <span id="pwd">Password:</span>
  <input type="password" id="pass">
</div>
<p>
  <button id="btnCheck">Check Password</button>
</p>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
-1

This is great for learning, but anyone can view source and see your password, so it's just for learning... Mmmkay? I've removed all the other logic so we can focus on the question. Once you solve all the issues mentioned by @ScottMarcus and others you should end up with something like this...

function check(x) {
  if (x === "HI") {
    alert('going to website')
    location.assign("#mywebsite");
  } else {
    alert("Nope... not gonna happen");
  }
}
<h3>Enter the Password</h3>
<b>Password:</b> 
<input type="password" id="pass" value="">
<button onclick="javascript:check(document.getElementById('pass').value)">Check Password</button>
Shanimal
  • 11,517
  • 7
  • 63
  • 76
  • 1
    If you were to add an explanation for what was wrong and why your code fixes the issue, it would be a good answer. Also, you have removed some of the code that seems to be relevant to the question. That should be put back in. – Scott Marcus Dec 28 '16 at 17:58
  • My colleagues here crack me up... If I have to sacrifice a few points to help a kid out Im fine with that. Sheesh. – Shanimal Dec 28 '16 at 18:06
  • But you have to remember that Stack Overflow is a knowledge base. We're not just trying to solve the programming problem, we are trying to write good answers that help others in the future who may have the same problem. Honestly, if someone in the future finds this page and your answer, they then have to read all of the other answers and comments to fully know what the issue is. That's not a good answer and it's not how SO works. **Or**, you could just provide a complete explanation for what you did and why. – Scott Marcus Dec 28 '16 at 18:08
  • Wait, you said if I explained what was wrong it would be a good answer. – Shanimal Dec 28 '16 at 18:10
  • But, you haven't done that. All your answer does is eliminate the actual code that is causing the problem without any explanations and wags your finger at using something like this for passwords. – Scott Marcus Dec 28 '16 at 18:11
  • I also think it was important to explain that the question needed to be simplified before it was posted which I did. The real problem with this question was too much code without enough explanation of what problem he was having. I think I accomplished that pretty well. – Shanimal Dec 28 '16 at 18:15
  • Again, if I have to sacrifice a few points to help the kid, Im good. – Shanimal Dec 28 '16 at 18:16
  • I don't see how an answer that removes code and doesn't provide any explanation helps anyone, but hey, that's your call. – Scott Marcus Dec 28 '16 at 18:21