Not signed in (Sign In)

Vanilla 1.1.5a is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthordbuschho
    • CommentTimeNov 18th 2009
     
    Validate a field within a numerical range

    use by:
    Including the code content in your custom code block. Then add binding code like the one shown below into your custom code block, in order to link the validation to a given FormAssembly field. Note the order of parameters is field_id, largest_value, smallest_value.

    <script>
    setup_range("tfa_Intbetween100and",200.4,-100.1);
    </script>


    example:
    http://www.tfaforms.com/127819

    code:
    <script>
    wFORMS.behaviors.validation.rules.isInRange = { selector: ".validate-range", check: 'validateRange'};

    wFORMS.behaviors.validation.messages.template_isInRange = "The value provided is not in the range between %%1%% and %%2%%";
    wFORMS.behaviors.validation.messages.isInRange = "The value provided is not in the range between %%1%% and %%2%%";


    wFORMS.behaviors.validation.instance.prototype.validateRange = function(element, value) {
    var _value = parseFloat(value);
    classAtts = element.className.split(" ");
    var _larger = null;
    var _smaller = null;
    for(i = 0; i<classAtts.length; i++){
    if(classAtts[i] == "validate-range"){
    if(classAtts[i+2]){
    _larger=parseFloat(classAtts[i+1]);
    _smaller=parseFloat(classAtts[i+2]);
    }
    }
    }

    if( (_value < _smaller) || (_value > _larger) ){
    wFORMS.behaviors.validation.messages.isInRange = wFORMS.behaviors.validation.messages.template_isInRange;
    wFORMS.behaviors.validation.messages.isInRange = wFORMS.behaviors.validation.messages.isInRange.replace('%%1%%',_larger);
    wFORMS.behaviors.validation.messages.isInRange = wFORMS.behaviors.validation.messages.isInRange.replace('%%2%%',_smaller);

    return false;
    }

    return true;
    }

    function setup_range(parent,larger,smaller){
    base2.DOM.Element.addEventListener(document,'DOMContentLoaded',function(){
    base2.DOM.bind(base2.DOM.Element.querySelector(document,"#"+parent)).addClass("validate-range " + larger + " " +smaller);
    });
    }
    </script>
    • CommentAuthorjlabau
    • CommentTimeNov 20th 2009
     
    I have also had good luck using regular expressions to enforce numerical ranges. Harder to write, yes, but it's built-in and you don't have to inject any more code.
    • CommentAuthordbuschho
    • CommentTimeNov 20th 2009
     
    Yup, but I always feel a bit bad handing non-programmers / power users something like:
    # 1 or 001..999:
    ^(0{0,2}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9])$
    http://www.regular-expressions.info/numericranges.html

    It's sort of an abuse of the regex power.
    Of course the code Ive provided is abusive in its own right as it relies on the order of class attributes, which I believe is not officially guaranteed.

    You pays your moneys and takes your chances.
    Drew.
    • CommentAuthorjlabau
    • CommentTimeNov 20th 2009
     
    So true! Not really what regexs were meant for, and they look like holy hell, but they seem to mesh pretty well with your validation tool :-)