0

The problem is that I can't get value of my password fields - alert with pso1 returns null.
I don't see any error in this code so I'm asking for help.

<form class='container' onsubmit="return checkit()">
    <h1> Javascript </h1>
    Login <input type='text' id='log'><br>
    Password <input type='password' id='pso1'><br>
    Confirm passwordd <input type='password' id='pso2'><br>
    <button>Register</button>
</form>

<script type="text/javascript">
    var pso1=document.getElementById('pso1').value ; 
    var pso2=document.getElementById('pso2').value ;    
    alert(pso1);
    function checkit(){
        if(pso1=!pso2){ 
            return false;
            alert('Passwords are not the same.');
        }
        else{
            return true; 
            alert('work');
        }
    }

</script>   

</body>
2
  • 3
    You're setting the value of the variables outside of the function, when the page loads. Commented Sep 16, 2016 at 22:17
  • Putting alert() after return won't work. return leaves the function and nothing after that executes. Commented Sep 16, 2016 at 23:35

2 Answers 2

1

In your code you are setting the values of pso1 and ps02 once right after the page load before their values are changed. You will want to update these values instead, declared them as local variables inside your function:

function checkit(){
    var pso1=document.getElementById('pso1').value; 
    var pso2=document.getElementById('pso2').value;   
    if(pso1=!pso2){ 
        return false;
        alert('Passwords are not the same.');
    }
    else{
        return true; alert('work');
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have only declared the function checkIt. You need to call that function to actually check it.

2 Comments

He's calling it here: <form class='container' onsubmit="return checkit()">
You should delete your answer if it's not relevant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.