1

Sure this is a simple problem but I am new to this. Am trying to develop a simple website for a user to work out a password and then if they get it right, take them to a congrats screen where they win a prize. However my if statement is not working in the code below

<script>
function CheckPassword() {
    var pword = document.getElementById("inpPassword").value;

    if (pword == ""|| pword == NULL) {
        alert("Please enter a password");
    } else if (pword == 'newman') {
        window.open('congratulations.html', '_self');
    } else {
        alert("Wrong password");    
    }
}
</script>

If I add an alert with the pword value then it prints out correctly what was entered. However the code picks up when the password input is blank, however if the input field is not blank then it does not go to the other screen or show the wrong password alert box. I have tried it with an alert box saying correct instead of the link to the new page but still did not work. Also tried using String(pword) in case that was the problem. Am sure this is a simple solution but just can't see it.

Thanks

2
  • Please provide the html to go with this so we can test your code. Also check the browser console I I suspect it will tell you the problem Commented Sep 21, 2017 at 12:49
  • Equivalent to if (pword == "" || pword == null) is if (!pword) Commented Sep 21, 2017 at 12:52

6 Answers 6

1

function CheckPassword() {
    var pword = document.getElementById("inpPassword").value;

    if (pword == ""|| pword == null){
        alert("Please enter a password");
    }
    else if (pword == 'newman') {
        window.open('congratulations.html', '_self');
    }
    else
    {
        alert("Wrong password");    
    }


}
<input type='password' id='inpPassword'>
<input type='button' onclick='CheckPassword()'>

if (pword == "" || pword == null) , its null not NULL . keywords are case sensitive.

Sign up to request clarification or add additional context in comments.

Comments

1

You should use null instead of NULL. Seems like it works besides of opening new window, because there is no congratulations.html in my snippet:

function CheckPassword() {
    var pword = document.getElementById("inpPassword").value;

    if (pword == ""|| pword == null){
        alert("Please enter a password");
    }
    else if (pword == 'newman') {
        window.open('congratulations.html', '_self');
    }
    else
    {
        alert("Wrong password");    
    }


}
<input id="inpPassword">
<button onclick="CheckPassword()">Click</button>

Comments

1

You may try here:

The mistake was here, you wrote NULL instead of null, beacuse of javascript understand null as a reserve keyword.

And use === for hard checking(data as well type checking of variaable)

function CheckPassword() {
    var pword = document.getElementById("inpPassword").value;

    if (pword == ""|| pword == null){
        alert("Please enter a password");
    }
    else if (pword === 'newman') {
        alert('congratulations.html', '_self');
    }
    else
    {
        alert("Wrong password");    
    }
}
<input type="" name="a" id="inpPassword">
<input type="button" onClick="CheckPassword()" value="check">

Comments

1

It seems the problem is when you compare to NULL. If you check your browser console you will likely get the following error:

Uncaught ReferenceError: NULL is not defined

That is not the correct syntax for null checks, it will look for a variable called "NULL" which you don't have. You should use null (lowercase) instead:

if (pword == ""|| pword == null){

The full adjusted code is as follows:

function CheckPassword() {
    var pword = document.getElementById("inpPassword").value;

    if (pword == ""|| pword == null) {
        alert("Please enter a password");
    } else if (pword == 'newman') {
        window.open('congratulations.html', '_self');
    } else {
        alert("Wrong password");    
    }
}

Here is a working example.

Comments

0

Null, "" are equivalent to false in Javascript. You just have to:

    if (!pword) {
        alert("Please enter a password");
    } else if (pword === 'newman'){
        window.open('congratulations.html', '_self');        
    } else {           
        alert("Wrong password");   
    }        

Comments

0

you can write null in small case so its work

function CheckPassword() {
var pword = document.getElementById("inpPassword").value;

if (pword == "" || pword == null ) {
  alert("Please enter a password");
} else if (pword == 'newman') {
  window.open('congratulations.html', '_self');
} else {
  alert("Wrong password");
}
}
<input id="inpPassword" onblur="CheckPassword()">

1 Comment

Thanks for all your help. Works fine now,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.