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

Checking form submission for minimum and maximum quantities

本文关键字:最大数 检查表 表格 提交 检查      更新时间:2023-09-26

使用现有脚本检查表单提交时的输入值是否存在最小数量。我需要包括一个条件来检查是否存在最大数量,并在不满足条件时显示错误消息。我相信这是一个编辑现有条件逻辑的问题,但就我的生命而言,我无法在不处理有效的最小条件的情况下让这些编辑发挥作用。与其提交我修改代码的失败尝试,不如提交一个只验证最小数量的功能版本。

$('#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']);
            }
        }
    });
}

});

应该是:

    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);
}

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