Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  119] [ 1]  / answers: 1 / hits: 25227  / 13 Years ago, fri, november 11, 2011, 12:00:00

I want to validate 2 input fields by comparing one to the other and make sure the second is greater than the first.



Do I need to add a custom method or can I just use the variable name in the range method? If so, can you point me to the syntax?



var validateSection = function (theForm) {
$(theForm).validate({
rules: {
startPoint: {
required: true,
range: [0, 100]
},
endPoint: {
required: true,
range: [startPoint + 1, 100] //Is this possible if I set the function to run on any change to either field?
},
}
});

if ($(theForm).valid()) {
return true;
}
else {
return false;
}
}


Code with Custom Method:



$.validator.addMethod(endGreaterThanBegin, function(value, element) {
return endPoint > startPoint
}, * End Point Should be Greater than Start);

var validateSection = function (theForm) {
$(theForm).validate({
rules: {
startPoint: {
required: true,
range: [0, 100]
},
endPoint: {
required: true,
range: [1, 100],
endGreaterThanBegin: true
},
}
});
if ($(theForm).valid()) {
return true;
}
else {
return false;
}
}


Getting $.validator is undefined before I get to form, and then validateSection is not a function when I start testing input fields


More From » jquery

 Answers
47

Need to add a custom validate method



Protoype -



$.validator.addMethod(endate_greater_startdate, function(value, element) {
return enddate > startdate
}, * Enddate should be greater than Startdate);


Validation -



endate_greater_startdate : true


Check for the Demo, as different example but will help debug.


[#89188] Thursday, November 10, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cadendericki

Total Points: 482
Total Questions: 109
Total Answers: 103

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
cadendericki questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Wed, Jul 8, 20, 00:00, 4 Years ago
Thu, May 14, 20, 00:00, 4 Years ago
;