-3

I'm working on recheck password while typing.Can anyone help me with the code that checks while typing password that shows a notification if it doesn't match entirely character by character while typing and that checks the length too when submit button is pressed in jquery or javascript

Alapati
  • 1
  • 1

3 Answers3

0

You can do this by several ways. This DEMO will solve your problem by using Jquery validation.

HTML

<form class="validatedForm" id="commentForm" method="get" action="">
<fieldset>
    <input name="user[password]" id="user_password" required/><br>
    <input name="user[password_confirmation]" required/>
</fieldset>
</form>
<button>Validate</button>

JQuery

jQuery('.validatedForm').validate({
rules: {
    "user[password]": {
        minlength: 3
    },
    "user[password_confirmation]": {
        minlength: 3,
        equalTo : "#user_password"
        }
    }
});

$('button').click(function () {
  console.log($('.validatedForm').valid());
});
acmsohail
  • 903
  • 10
  • 32
0

Original answer - https://stackoverflow.com/a/9717644/7643022

That answer gives you the solution to what you need. I have just modified the answer to what you desire.

html

<div class="td">
    <input type="password" id="txtNewPassword" />
</div>
<div class="td">
    <input type="password" id="txtConfirmPassword" onChange = "checkPasswordMatch();" />
</div>
    <div class="registrationFormAlert" id="divCheckPasswordMatch">
</div>
<div><input type="submit" id="submitbtn"/></div>

JQuery

var incorrectFlag = false;
function checkPasswordMatch() {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword)
        incorrectFlag = true;
    else
        incorrectFlag = false;
}

$(document).ready(function () {
   $("#txtConfirmPassword").keyup(checkPasswordMatch);
   $("#submitbtn").onclick(function(e){
       e.preventDefault();
       if (incorrectFlag){
           alert("Password Incorrect");
       } else {
           $('form').submit();
       }
   });
});
Sagar
  • 477
  • 4
  • 15
0

The Actual password should be retrieved and stored somewhere, here I assumed it should be stored in the hidden input.

$(document.ready(
    var actual_password = $("#hidden_input_password").val();
    $( "#password_text_box" ).keyup(function(event) {
          var input_Password = $(this).val();
          if(input_Password.length > actual_password.length)
          {
              event.preventDefault();
              event.stopPropogation();
              return;
          }
          elseif(input_Password.length === actual_password.length){
              if(input_Password===actual_password) 
              {
                 return;
              }  
              else{
                event.preventDefault();
                event.stopPropogation();
                $(this).addClass("notification");
                return;
              }
           } 
           else{
                     if(input_Password!===actual_password.slice(0,input_Password.length))
    {
                event.preventDefault();
                event.stopPropogation();
                $(this).addClass("notification");
                return;

     }
           }
    });
    );