-2

Hi i have to validate password and confirm password, my constraint is when ever i visit a page confirm password textbox should be disabled,

when i click on submit required message only for password field it should not validate the confirm password ,

when i entered right password in my password field then only confirm password field should be enabled

right password means it should contain at leaset 8 characters in that 1 upper case letter,1 lower case letter,1 number,1 special character

after entered confirm password and click on check should validate both are same

  <form id="formCheckPassword">
        <input type="password"  name="password" id="password"/>
        <p id="passerror"></p>    
        <br>
        <input type="password"  name="cfmPassword" id="cfmPassword" disabled /><br>
        <input type="submit" value="submit" id="s"/>
     </form>

Jquery

  $(document).ready(function() {
                $("#s").click(function(event) {
          var passerror = "";
          var pass =  $.trim($("#password").val());
          if (pass == "") {
                        passerror = "pass is required";
                        $("#passerror").html(passerror);
                        $("#passerror").show();
                        event.preventDefault();
                    } 
            if(pass!= cfmPassword)
            {
             display error message

            }
            else
            {

            }
       });

      $("#password").focusout(function(){
          var pwd=$("#password").val();
                var errorMessage = "";
                    function isValidPassword(emailAddress) {
                      var pattern = new RegExp(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/);
                      return pattern.test(emailAddress);
                    };
                    if (!isValidPassword(pwd.val())) {
                      errorMessage = "<br />Please enter a valid email address";
                      $("#error").html(errorMessage);
                      $( "#error" ).show();
                      event.preventDefault();

                    }

        });     
            });

css

  #passerror{  
        color: red;
    }

Can anyone update the code

http://jsfiddle.net/BSdc8/285/

mantelinga r
  • 440
  • 1
  • 5
  • 20
  • it's not advisable to validate it in your js side, it can bypass easily validate it inside your backend – Beginner Nov 23 '17 at 06:40
  • @Beginner My interpretation is that the client validation would only be to ensure that the password conforms to the password rules, and matches the validation field. The actual checking whether the password is correct would be done on the server side. – Peter Abolins Nov 23 '17 at 06:41

1 Answers1

2

jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});

// function to validate first password

$.validator.addMethod("pwcheck", function(value) {
return /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})/.test(value)
});

$( "#myform" ).validate({
  rules: {
     password:{
 required:true,
 minlength:8,
 pwcheck:true
 },
    password_again:{
 minlength:8,
 equalTo :'#Password'
 }
  }
});
<html>
<head>
<meta charset="utf-8">
<title>Makes "field" required to be the same as #other</title>
<link rel="stylesheet" href="https://jqueryvalidation.org/files/demo/site-demos.css">
 
</head>
<body>
<form id="myform">
<label for="password">Password</label>
<input id="password" name="password" />
<br/>
<label for="password_again">Again</label>
<input class="left" id="password_again" name="password_again" />
<br>
<input type="submit" value="Validate!">
</form>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
</body>
</html>
Cruzer
  • 405
  • 5
  • 13