jQWidgets Forums

jQuery UI Widgets Forums Plugins Validator, Drag & Drop, Sortable Passing parameters to Custom Validator

This topic contains 1 reply, has 1 voice, and was last updated by  mohamedazher 10 years, 12 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author

  • mohamedazher
    Participant

    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


    mohamedazher
    Participant

    Hi,

    Please ignore this, i solved this by wrapping the rule creation in a closure.

    -Mohamed Azher

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.