Tuesday, 4 April 2017

Store input value as a variable and then compare

I'm trying to compare 2 input values in a form to check if they are the same.

my form is as follows:

    <div class="form-group">
    <i class="fa fa-key" aria-hidden="true"></i>
    <label for="password1">Password</label>
        <input id="password1" type = "password" class ="form-control" placeholder = "Enter password" name="password1">
</div>
<div class="form-group">
    <i class="fa fa-key" aria-hidden="true"></i>
    <label for="password2">Confirm password</label>
        <input id="password2" type = "password" class ="form-control" placeholder = "Enter password" name = "password">
</div>
<div class="form-group">
<i class="fa fa-picture-o" aria-hidden="true"></i>
    <label for="img">Image</label>
        <input type = "text" class ="form-control" placeholder = "Enter image URL" name = "image">
</div>
    <button id="submit-login" type ="submit" class="btn btn-primary btn-lg">Signup</button>
</form>
</div>
<button id="validate">Validate</button>

Ultimately I want to prevent the default action when submitting but I put the validate button in for now just to more easily check if I'm capturing the right info.

I wrote the following jQuery:

// store password inputs as variables
$('#password1').keyup(function(){
  var value1 = $("#password1").val(); 
  $("#password1").val(value1);
});

$('#password2').keyup(function(){
  var value2 = $("#password2").val();
  $("#password2").val(value2);
});

//  check if passwords match

$('#validate').on('click', function(){
if(value1 = value2){
    alert("Yes they match")
} else {
    alert("Passwords do not match")
}
})

However it is not working. I tried adding a console.log into the code where I am trying to capture the inputs and it is indeed storing the logs but it doesn't seem to be storing the final value which I think may be causing the issue here.



via DaveB1

No comments:

Post a Comment