2

I have written the below code to make a simple form for validation of form inputs through javascript. Here username and passwords are written in the JS code, but it still shows alert message of the else loop even if giving correct credentials on the form. Please Help?

var user = document.getElementById('username')
var pass = document.getElementById('password')

function user1() {
  if (user == "admin" && pass == "root") {
    window.open("javascript_trial.html")
    alert('correct username')
  } else {
    alert('incorrect username or password')
  }
}
<form>
  <input type="text" name="username" Placeholder="enter username"><br>
  <input type="password" name="password" Placeholder="enter password"><br>
  <button onclick="user1()">Submit</button>
</form>

2
  • 3
    getElementById you don't have any id's on the elements, you have names Commented Dec 18, 2017 at 14:49
  • 1
    Looking at this code, I hope this is just to learn JavaScript/HTML. Commented Dec 18, 2017 at 16:00

1 Answer 1

1

There are a few errors here:

  • You need to get the values of your inputs
  • You want to get those values when the button is clicked. Your code is grabbing them only when the page loads. Move the variable assignment into your function
  • You didn't give the elements ID attributes

function user1() {
  var user = document.getElementById('username').value
  var pass = document.getElementById('password').value
  if (user == "admin" && pass == "root") {
    window.open("javascript_trial.html")
    alert('correct username')
  } else {
    alert('incorrect username or password')
  }
}
<form>
  <input type="text" name="username" id="username" Placeholder="enter username"><br>
  <input type="password" name="password" id="password" Placeholder="enter password"><br>
  <button onclick="user1()">Submit</button>
</form>

Also note that a button's default type is submit which will submit your form and reload the page after the alert is shown when clicked, so you might want to change that to type="button" to prevent that.

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

4 Comments

Your code is grabbing them only when the page loads. Move the variable assignment into your function you don't have to, you can keep the elements as global, when you call .value it will get the current value. It depends on if the OP is planning on using them elsewhere.
You want to get those values when the button is clicked. Your code is grabbing them only when the page loads. He is not grabbing any values, not on click, not on page-load. He is just assigning the elements to variables (which also won't work as he didn't assign the ID attribute).
@Abbas Essentially we agree. The OP's code won't do anything since he's not getting the values. If the code is in the head, and he did grab the values, he'd get undefined since the elements don't exist yet. If he put the JS at the end of the page or wrapped it in a function to run after the DOM was ready (and got the value) then he'd get whatever value the input had at that time, not after the user changed it.
@George If the OP did something like var user = document.getElementById('username').value outside the function then he'd get undefined every time no matter the input. If he did var user = document.getElementById('username') and got the value in the function with user.value then yes, that'd be fine.

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.