Hi,
I have a problem referencing variables inside the custom validator function. In my code, I add validations dynamically to inputs. I have a custom greaterThan validator which compares the current input’s value to a another input (whose id is dynamic).
if(/^greaterThan=[a-zA-Z0-9_\.\/]+$/.test(currentRule){
var secondElement = currentRule.match(/[a-zA-Z0-9_\.\/]+$/)[0];
rule = {input: “#”+firstElement,
message: firstElementID + ‘ should be greater than ‘+secondElement
action: ‘keyup, blur’,
rule: function (input, commit, secondElement) {
var elementFirst = $(input.selector).val();
var elementSecond = $(“#”+secondElement).val();
result = false;
if (elementFirst > elementSecond) {
result = true
} else{
return false;
}
In the above code, i tried passing secondElement through the custom function but since i loop through and add many such rules to different elements, the secondElement inside the rule function has reference to the last value it got and not the one while adding the rule.
Hope you can help me with this.
Regards,
Mohamed Azher