jQuery验证大于&少于

时间:2012-10-03 12:42:31

标签: jquery asp.net-mvc-3 validation jquery-validate

如何确保用户无法输入小于或大于某个值的值?

问题在于验证具有比另一个字段更小或更大的值的字段。

2 个答案:

答案 0 :(得分:2)

如果您使用的是jquery.validation插件,那么这是更大的方法

$.validator.addMethod(
"greaterThan",
function(value, element, params) {
    var target = $(params[0]).val();
    var isValueNumeric = !isNaN(parseFloat(value)) && isFinite(value);
    var isTargetNumeric = !isNaN(parseFloat(target)) && isFinite(target);
    if (isValueNumeric && isTargetNumeric) {
        return Number(value) > Number(target);
    }

    if (!/Invalid|NaN/.test(new Date(value))) {
        return new Date(value) > new Date(target);
    }

    return false;
},
'Must be greater than {1}.');

答案 1 :(得分:0)

    $(document).ready
(
    function ()
    {
        $.validator.addMethod(
        "greaterThan",
        function (value, element, param)
        {
            // bind to the blur event of the target in order to revalidate whenever the target field is updated            
            var target = $(param)
            .unbind(".validate-greaterThan")
            .bind
            (
                "blur.validate-greaterThan", 
                function ()
                {
                    $(element).valid();
                }
            );
            return parseFloat(value) >= parseFloat(target.val());
        },
        "Pretul trebuie sa fie mai mare decat valoarea initiala"
        ); 
    }
);



$('#gvProduseList input[name$=Pret]').each
    (
        function (index, domEle)
        {
            $(this).rules
            (
                "add"
                , {

                    required: true,
                    minlength: 1,
                    range: [0.1, Number.MAX_VALUE],
                    greaterThan: '#Produse_' + index + '__PretIntrare',
                    messages: 
                    {
                        required: "Pretul este necesar!",
                        minlength: "Pretul este necesar!",
                        range: "Pretul este necesar!",
                        greaterThan: "Pretul trebuie sa fie mai mare decat " + $('#Produse_' + index + '__PretIntrare').val()
                    }
                }
            );
        }
    );




$(document).ready
(
    function ()
    {
        $.validator.addMethod(
        "lessThan",
        function (value, element, param)
        {
            // bind to the blur event of the target in order to revalidate whenever the target field is updated            
            var target = $(param)
            .unbind(".validate-lessThan")
            .bind
            (
                "blur.validate-lessThan",
                function ()
                {
                    $(element).valid();
                }
            );
            return parseFloat(value) <= parseFloat(target.val());
        },
        "Valoarea trebuie sa fie mai mica sau egala decat valoarea initiala"
        );
    }
);


 $('#gvListDetaliiElemTranAdaugare input[name$=Valoare]').each
    (
        function (index, domEle)
        {
            $(this).rules
            (
                "add"
                , {

                    required: true,
                    minlength: 1,
                    range: [0.1, Number.MAX_VALUE],
                    lessThan: '#ListaDetaliiElemTranModelAdaugare_' + index + '__ValoareRamasa',
                    messages:
                    {
                        required: "Valoarea este necesara!",
                        minlength: "Valoarea este necesara!",
                        range: "Valoarea este necesara!",
                        lessThan: "Valoarea trebuie sa fie mai mica sau egala cu " + $('#ListaDetaliiElemTranModelAdaugare_' + index + '__ValoareRamasa').val()


                    }
                }
            );
        }
    );