检查表单提交的最小和最大数量

时间:2015-10-17 18:56:33

标签: javascript php jquery forms

使用现有脚本检查表单提交上的输入值是否存在最小数量。我需要包含一个条件来检查是否存在最大数量,并在不满足条件时显示错误消息。我认为这是编辑现有条件逻辑的问题,但对于我的生活,我无法在不影响功能最小条件的情况下进行编辑。而不是提交我未能改变代码的尝试,而是提交仅验证最小数量的功能版本。

$('#button-cart').on('click', function() {
    var total_quantity = 0;
    var min_oneside = parseInt("<?php echo $min_oneside; ?>");
    var min_bothside = parseInt("<?php echo $min_bothside; ?>");
    var min_noprint = parseInt("<?php echo $min_noprint; ?>");
    var min_standard = parseInt(" <?php echo $minimum; ?>");
    var max_standard = parseInt(" <?php echo $maximum; ?>");
    var validQty = true;
    var validMaxQty = true;
    var validInput = true;      
    var oth_minimum = "Quantity must be at least <?php echo $minimum; ?>";
    var oth_maximum = "Quantity must be less than <?php echo $maximum; ?>"

    $('#content').find('input[name^="option-quantity"]').each(function(){
            total_quantity = total_quantity + parseInt($(this).val());
    });
    $('#content').find('input[name="quantity"]').each(function(){
            total_quantity = total_quantity + parseInt($(this).val());
    });

    var element = $("label:contains('Printing')").attr('for');
    if(element === undefined){
        if( total_quantity < min_standard ) {
            validQty = false;
            validInput = false;
            oth_minimum = "Quantity must be at least " + min_standard;
        }else if (total_quantity > max_standard){
            validQty = false;
            validInput = false;
            oth_message = oth_maximum;
            }
    } else {
        if ( ($("#" + element + " option:selected").text()).match("Printed One Side") ){
            if( total_quantity < min_oneside ) {
                validQty = false;
                validInput = false;
                oth_minimum = "Quantity must be at least " + min_oneside;
            }else if (total_quantity > max_standard){
                validQty = false;
                validInput = false;
                oth_message = oth_maximum;
            }
        }
        if ( ($("#" + element + " option:selected").text()).match("Printed Both Sides") ){
            if( total_quantity < min_bothside ) {
                validQty = false;
                validInput = false;
                oth_minimum = "Quantity must be at least " + min_bothside;
            }else if (total_quantity > max_standard){
                validQty = false;
                validInput = false;
                oth_message = oth_maximum;
            }
        }
        if ( ($("#" + element + " option:selected").text()).match("No Printing") ){
            if( total_quantity < min_noprint ) {
                validQty = false;
                validInput = false;
                oth_minimum = "Quantity must be at least " + min_noprint;
            }else if (total_quantity > max_standard){
                validQty = false;
                validInput = false;
                oth_message = oth_maximum;
            }
        }
    }
    if(!validQty) {
        $('#errorQuantity').html(oth_minimum);
    }
    var minimum = "Quantity must be at least <?php echo $minimum; ?>";
    if($('#errorQuantity').length > 0 && validQty) {
        var quantity = 0;
        $('#errorQuantity').empty();
        $('input[type=number]').each(function () {
            var currentId = $(this).attr('id');
            checkboxId = currentId.replace("quantity-", "");
            if(isNaN($(this).val()) || $(this).val() < 0) {
                $(this).focus();
                $(this).css('background-color', 'red');
                validInput = false;
            } else {
                quantity += parseInt($(this).val());
            }
        });
        if(quantity <= 0) {
            $('#errorQuantity').append(minimum);
            validInput = false;
        }
    }
    if(validInput){
        $('input[type=number]').each(function () {
            $(this).css('background-color', '#FFFFFF');
        });
        $('#errorQuantity').empty();

        $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: $('.product-info input[type=\'text\'], .product-info input[type=\'number\'], .product-info input[type=\'date\'], .product-info input[type=\'datetime\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
            dataType: 'json',
            beforeSend: function() {
                $('#button-cart').attr('disabled', true);
                $('#button-cart').after('<i class="fa fa-circle-o-notch fa-spin"></i>');
            },
            success: function(json) {
                $('.alert, .text-danger').remove();
                $('.form-group').removeClass('has-error');
                $('#button-cart').next('.fa-spin').remove();
                $('#button-cart').attr('disabled', false);
                if (json['error']) {
                    var errors = '';
                    if (json['error']['option']) {
                        for (i in json['error']['option']) {
                            var element = $('#input-option' + i.replace('_', '-'));
                            element.parents('.form-group').first().find('> label + div').append('<div class="text-danger">' + json['error']['option'][i] + '</div>');
                        }
                    }
                if (json['error']['recurring']) {
                    $('select[name="recurring_id"]').after('<span class="error">' + json['error']['recurring'] + '</span>');
                }
                // Highlight any found errors
                $('.text-danger').each(function() {
                    $(this).parents('.form-group').first().addClass('has-error');
                });
            }
            if (json['success']) {
                $('.tb_widget_cart > .tb_nav').load('index.php?route=common/cart/info .tb_nav > *');
                window.location = $('base').attr('href') + 'index.php?route=checkout/cart';
                //displayNotice('product', 'success', 'product', json['success']);
            }
        }
    });
}

});

1 个答案:

答案 0 :(得分:0)

应该是这样的:

    if(element === undefined){
    if( total_quantity < min_standard ) {
        validQty = false;
        validInput = false;
        oth_message = oth_minimum;
    }else if (total_quantity > max_standard){
        validQty = false;
        validInput = false;
        oth_message = oth_maximum;
    }
} else {
    if ( ($("#" + element + " option:selected").text()).match("Printed One Side") ){
        if( total_quantity < min_oneside ) {
            validQty = false;
            validInput = false;
            oth_message = "Quantity must be at least " + min_oneside;
        }
    }
    if ( ($("#" + element + " option:selected").text()).match("Printed Both Sides") ){
        if( total_quantity < min_bothside ) {
            validQty = false;
            validInput = false;
            oth_message = "Quantity must be at least " + min_bothside;
        }
    }
    if ( ($("#" + element + " option:selected").text()).match("No Printing") ){
        if( total_quantity < min_noprint ) {
            validQty = false;
            validInput = false;
            oth_message = "Quantity must be at least " + min_noprint;
        }
    }
}
if(!validQty) {
    $('#errorQuantity').html(oth_message);
}

您可以为其他条件添加相同的方式,我只为第一个条件添加。