0

I'm comparing password in javascript and someone its not working its working on my past project before here is the HTML

<form>
        <label>
            <strong>Username</strong>
            <input type="text" name="">
        </label>
        <label>
            <strong>Password</strong>
            <input type="password" name="" id="password">
        </label>
        <label>
            <strong>Confirm Password</strong>
            <input type="password" name="" id="confirmpassword"> 
        </label>
        <button class="button" type="submit" id="button" onclick="click();">Submit</button>
    </form>

and here is javascript

function click (){
    var password =  document.getElementID('password').value,
    confirmpassword =   document.getElementID('confirmpassword').value

    if (password == ""){
        alert("Field cannot be empty.");

        
    }
    else if (password != confirmpassword){
        alert("Password didnt match try again.");
        return false

    }
    else if(password == confirmpassword){
        alert("Password Match")
        
    }
    return false
}
Sarun UK
  • 6,210
  • 7
  • 23
  • 48

4 Answers4

2

You are using getElementID to get password field value, but there is no document.getElementID function.

It should be document.getElementById to get that field value.

Everest Climber
  • 1,203
  • 1
  • 9
  • 15
0

Try adding onSubmit to the form tag and call the function there instead of in the submit button Example:

<form name="myForm" onsubmit="return click();"> 

And remove the onClick from submit button

And it is not getElementID. It is getElementById()

Vishnu Vinod
  • 603
  • 4
  • 15
0

You can try this:

HTML:

<form id="form-id">
        <label>
            <strong>Username</strong>
            <input type="text" name="">
        </label>
        <label>
            <strong>Password</strong>
            <input type="password" name="" id="password">
        </label>
        <label>
            <strong>Confirm Password</strong>
            <input type="password" name="" id="confirmpassword"> 
        </label>
        <button id="your-id">submit</button>
    </form>

JS:

var form = document.getElementById("form-id");

document.getElementById("your-id").addEventListener("click", function () {
  var password = document.getElementById("password").value,
    confirmpassword = document.getElementById("confirmpassword").value;

  if (password == "") {
    alert("Field cannot be empty.");
  } else if (password != confirmpassword) {
    alert("Password didnt match try again.");
    return false;
  } else if (password == confirmpassword) {
    alert("Password Match");
    form.submit();
  }
});
Swaraj Gandhi
  • 682
  • 3
  • 15
0

you should change your function name because 'click' not working change it to for example submit and it works fine;

function submit (){
    var password =  document.getElementById('password').value,
    confirmpassword =   document.getElementById('confirmpassword').value

    if (password == ""){
        alert("Field cannot be empty.");

        
    }
    else if (password != confirmpassword){
        alert("Password didnt match try again.");
        return false

    }
    else if(password == confirmpassword){
        alert("Password Match")
        
    }
    return false
}
        <label>
            <strong>Username</strong>
            <input type="text" name="">
        </label>
        <label>
            <strong>Password</strong>
            <input type="password" name="" id="password">
        </label>
        <label>
            <strong>Confirm Password</strong>
            <input type="password" name="" id="confirmpassword"> 
        </label>
        <button class="button" type="button" id="button" onclick="submit();">Submit</button>
barzin.A
  • 1,554
  • 2
  • 12
  • 20