Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
17
rated 0 times [  19] [ 2]  / answers: 1 / hits: 15787  / 10 Years ago, sat, october 4, 2014, 12:00:00

I validate my form using jquery validate like this:



$(#formuser).validate({

rules: {

accountname: {
required: true,
minlength: 6
},
T1: {
required: true,
minlength: 6
},
accountpassword1: {
required: true,
minlength: 8
},
accountpassword2: {
required: true,
minlength: 8,
equalTo: #accountpassword1
},
},
});


HTML:



<input id=accountpassword1 class=text-input form-control  type=text name=accountpassword1 size=24 placeholder=password >

<input class=text-input form-control type=text name=accountpassword2 size=24 placeholder=password >


this worked for me But. I need to check jquery validate when password1 and password 2 not empty. my mean is if password input is empty jquery validate not validate field else validate field password1 and password2.



how do create this?



NOTE:: i need to only check validate accountpassword1 and accountpassword1 with this method.


More From » jquery

 Answers
4

You can use the depends option like



function isPasswordPresent() {
return $('#accountpassword1').val().length > 0;
}

$(#formuser).validate({
rules: {
accountname: {
required: true,
minlength: 6
},
T1: {
required: true,
minlength: 6
},
accountpassword1: {
//required: true is not required
minlength: {
depends: isPasswordPresent,
param: 8
}
},
accountpassword2: {
required: isPasswordPresent,
minlength: {
depends: isPasswordPresent,
param: 8
},
equalTo: {
depends: isPasswordPresent,
param: #accountpassword1
}
},
},
});


Demo: Fiddle


[#69245] Wednesday, October 1, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
timothyc

Total Points: 233
Total Questions: 103
Total Answers: 103

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
;