验证方法returns true not false

Validation method returns true not false

我正在创建一个 jQuery 验证方法,用于二维字段(宽度和高度)。用户可以在页面的 select 框中选择使用毫米或英寸。这些字段验证了以下几点:

工作原理: 当一个字段被使用时,一个 ajax 调用将使用该字段的值完成。该值进入 PHP 函数,该函数将其转换为以毫米为单位的大小。这给出了以毫米为单位的正确结果。

当这个值(以毫米为单位)返回时,它会通过一些条件语句来检查验证点。然后将结果变量设置为 true 或 false,最后脚本 returns it.

问题: 当我将尺寸设置为 "inches",并在第一个字段中填写 38 (965,19mm),在第二个字段中也填写 38 时,脚本 returns 为真。这不应该发生,因为我的 if 语句说如果一个区域超过 860mm,另一个区域可能不会超过 860mm。奇怪的是,使用毫米作为尺寸,一切正常..

我的Javascript:

/** Dimension restrictions validator method */
jQuery.validator.addMethod( 'restrictions', function(value, element) {

    // Set variables
    var result = true;
    var message = '';
    var other = $(element).parents().siblings('.input-group').find('.dimension-field').val();   
    var mUnit = $('#size_data option:selected').val(); 

    // Do ajax call to get millimeters
    $.ajax({
        url: sbt.ajaxurl,
        async: false,
        data: { action: 'sbt_mm_value', value: value, mu: mUnit },
        success: function(data) {

            // Smaller than 980
            if(data <= 980) {

                // Bigger than 100                                  
                if(data >= 100) {
                    result = true;
                } else {
                    result = false;

                    if(mUnit == 'in') {
                        message = sbt.msg.restrictions.minimum + ' 3.93in';
                    } else {
                        message = sbt.msg.restrictions.minimum + ' 100mm';
                    }
                }

                // Bigger than 860
                if(data > 860) {

                    // Other field smaller than or equal than 860
                    if(other <= 860) {
                        result = true;
                    } else {
                        result = false;
                        if(mUnit == 'in') {
                            message = sbt.msg.restrictions.maximum + ' 33.85in';    
                        } else {
                            message = sbt.msg.restrictions.maximum + ' 860mm';  
                        }
                    }
                } 

                // Smaller than or equal to 860
                if(data <= 860) {

                    // Other field smaller than or equal than 980
                    if(other <= 980) {
                        result = true;  
                    } else {
                        result = false;
                        if(mUnit == 'in') {
                            message = sbt.msg.restrictions.maximum + ' 33.85in';    
                        } else {
                            message = sbt.msg.restrictions.maximum + ' 860mm';  
                        }
                    }
                }

            } else {
                result = false;
                if(mUnit == 'in') {
                    message = sbt.msg.restrictions.maximum + ' 38.58in';    
                } else {
                    message = sbt.msg.restrictions.maximum + ' 980mm';  
                }
            }


        }
    });

    // Set message
    $.validator.messages.restrictions = message;

    // Return true or false
    return result;

});

将方法应用于字段:

$('#action_form').validate({
    rules: {
        width_data: { 
            required: true,
            restrictions: true,

        },
        height_data: { 
            required: true,
            restrictions: true,
        },
    },
});

我自己找到了解决办法。问题是由于其他字段值未转换为毫米引起的。这就是为什么它选择毫米作为尺寸而不是英寸的原因,因为毫米已经是毫米,但英寸不是。

/** Dimension restrictions validator method */
jQuery.validator.addMethod( 'restrictions', function(value, element) {

    // Gets selected size
    var mUnit   = $('#size_data option:selected').val(); 

    // Gets values of both fields
    var otherField  = $(element).parents().siblings('.input-group').find('.dimension-field').val().toString();  
    var thisField   = value.toString();

    // Set default variables
    var result  = true;
    var message     = '';

    /** Does AJAX call to get millimeters and does checks */
    $.ajax({
        url: sbt.ajaxurl,
        async: false,
        data: { action: 'sbt_mm_value', mu: mUnit },
        success: function(data) {

            // Gets one unit in millimeters
            var multiply = data.toString();

            // Get millimeter values
            var value = thisField * multiply;
            var other = otherField * multiply;

            //console.log('Value = ' + value + ' and other is '+ other);

            // Smaller than 980
            if(value <= 980) {

                // Greater than 100                                 
                if(value >= 100) {
                    result = true;
                } else {
                    result = false;

                    if(mUnit == 'in') {
                        message = sbt.msg.restrictions.minimum + ' 3.93in';
                    } else {
                        message = sbt.msg.restrictions.minimum + ' 100mm';
                    }
                }

                // Greater than 860
                if(value > 860) {

                    // Other field smaller than or equal than 860
                    if(other <= 860) {
                        result = true;
                    } else {
                        result = false;
                        if(mUnit == 'in') {
                            message = sbt.msg.restrictions.maximum + ' 33.85in';    
                        } else {
                            message = sbt.msg.restrictions.maximum + ' 860mm';  
                        }
                    }
                } 

                // Smaller than or equal to 860
                if(value <= 860) {

                    // Other field smaller than or equal than 980
                    if(other <= 980) {
                        result = true;  
                    } else {
                        result = false;
                        if(mUnit == 'in') {
                            message = sbt.msg.restrictions.maximum + ' 33.85in';    
                        } else {
                            message = sbt.msg.restrictions.maximum + ' 860mm';  
                        }
                    }
                }

            } else {
                result = false;
                if(mUnit == 'in') {
                    message = sbt.msg.restrictions.maximum + ' 38.58in';    
                } else {
                    message = sbt.msg.restrictions.maximum + ' 980mm';  
                }
            }


        }
    });

    // Set message
    $.validator.messages.restrictions = message;

    // Return true or false
    return result;

});