views:

412

answers:

1

I have a form which have three fields

  1. Old Password
  2. New Password
  3. Confirm Password

These fields are optional and only mandatory when user enter some text into New Password How can i apply jQuery Validator plugin for the same.

       jQuery.validator.addMethod("pass2",
  function(value,element,param) {
  var op=document.forms[0].oldpass;
  var newp=document.forms[0].newpass1;
  var cp=document.forms[0].newpass2;
  if(newp.value != "" )
  { if(element.name=="newpass2")
   {  if(cp.value!=newp.value) {return true;}
      else {return false;}

   }
   if(element.name=="oldpass")
   {   if(op.value=="") {return true;}
       else { return false;}
   }
  }
  else
       return true;
 },"");


$(document).ready(function() {
  $("#form1").validate({
debug:true,
    rules: {
      oldpass:
  {
 pass2:true
  },
  /*newpass1:
  {
 pass1:true
  },*/
  newpass2: {
 pass2:true

  }
    },
    messages: {
      oldpass: "Please enter a oldpass",
  //newpass1: "Please enter newpassword",
  newpass2: "Please confirm password"
    }
  });
});

">

+2  A: 

You need to add your own validation method for that.

This allows you to specify a javascript function that determines if a given input field is valid or not. In this case, the function would return false if the NewPassword is non-empty and the given field is empty. Attach that method to your OldPassword and ConfirmPassword fields and you are done.

Adrian Grigore