简体   繁体   English

将输入中的数据收集到数组中以进行验证

[英]Collect data from inputs into array for validation

I am looking for a way to loop through a group of inputs, appending the data from each into an array so that I might check to see if the array is empty, thus allowing me to validate if one or more of my inputs has been filled out. 我正在寻找一种遍历一组输入的方法,将每个输入的数据追加到数组中,以便检查数组是否为空,从而使我可以验证是否已填充一个或多个输入出来。 I am using parsley for my validation, but the customer wants a way to ensure that AT LEAST ONE of these elements is filled out. 我正在使用香菜进行验证,但是客户希望找到一种方法来确保至少填写这些元素。

Currently I have it working as a giant jQuery requirement/rules setup. 目前,我将其作为巨型jQuery需求/规则设置进行工作。 But I feel it is clunky and my other idea would save space and time. 但是我觉得这很笨拙,我的其他想法可以节省空间和时间。

My jsfiddle 我的jsfiddle

// validate form    
$("#signupform").validate({
    rules: {
        telephone: {
            required: function (element) {
                if (($("#mobile").val().length > 0) || ($("#olb").val().length > 0) || ($("#ach").val().length > 0)|| ($("#internet").val().length > 0) || ($("#paper").val().length > 0) ) {
                    return false;
                } else {
                    return true;
                }
            }
        },
        mobile: {
            required: function (element) {
                if (($("#telephone").val().length > 0) || ($("#olb").val().length > 0) || ($("#ach").val().length > 0)|| ($("#internet").val().length > 0) || ($("#paper").val().length > 0) ) {
                    return false;
                } else {
                    return true;
                }

            }
        },
        olb: {
            required: function (element) {
                if (($("#telephone").val().length > 0) || ($("#mobile").val().length > 0) || ($("#ach").val().length > 0)|| ($("#internet").val().length > 0) || ($("#paper").val().length > 0) ) {
                    return false;
                } else {
                    return true;
                }
            }
        },
        ach: {
            required: function(element) {
                if (($("#telephone").val().length > 0) || ($("#olb").val().length > 0) || ($("#mobile").val().length > 0)|| ($("#internet").val().length > 0) || ($("#paper").val().length > 0) ) {
                    return false;
                } else {
                    return true;
                }
            }
        },
        internet: {
            required: function(element) {
                if (($("#telephone").val().length > 0) || ($("#olb").val().length > 0) || ($("#ach").val().length > 0)|| ($("#mobile").val().length > 0) || ($("#paper").val().length > 0) ) {
                    return false;
                } else {
                    return true;
                }
            }
        },
        paper: {
            required: function(element) {
                if (($("#telephone").val().length > 0) || ($("#olb").val().length > 0) || ($("#ach").val().length > 0)|| ($("#internet").val().length > 0) || ($("#mobile").val().length > 0) ) {
                    return false;
                } else {
                    return true;
                }
            }
        }
    }
});

This doesn't fit with the validation plugin, but here's a simple solution: 这与验证插件不匹配,但这是一个简单的解决方案:

http://jsfiddle.net/yKRdh/ http://jsfiddle.net/yKRdh/

var atLeastOneFilled = $('input:not([type="submit"]').filter(function () {
    return $.trim(this.value) !== '';
}).length > 0;

I'm not familiar with parsley, but this jQuery will tell you if you have at least one input filled out or not: 我对欧芹不熟悉,但是此jQuery会告诉您是否填写了至少一个输入:

var atLeastOneFilledOut = $(':input')
    .filter(function() {
        var val = $(this).val();
        return val != "" && val != 0;
    }).length;

Maybe you could use the above function in a form-level rule. 也许您可以在表单级规则中使用上述功能。

Ended up doing this ( jsfiddle ): 最终做到了这一点( jsfiddle ):

<form id="signupform">
    <select id="masterSelect">
        <option value="">nuttin</option>
        <option value="stuff">asdfasdf</option>
    </select>
    <div class="required-group">
        <input id="telephone" name="telephone" />
        <br/>
        <input id="mobile" name="mobile" />
        <br/>
        <input id="ach" name="ach" />
        <br />
        <input id="internet" name="internet" />
        <br />
        <input id="paper" name="paper" />
    </div>
    <button id="submit" type="submit">Submit</button>
</form>
<script>
$('#masterSelect').on('change', function () {
    if (this.value == "stuff") {
        $('#submit').attr("disabled", "disabled");
    } else {
        $('#submit').removeAttr("disabled");
    }
});
$('.required-group input').on('change', function () {
    var atLeastOneFilled = false;
    atLeastOneFilled = $(this).filter(function () {
        return $.trim(this.value) !== '';
    }).length > 0;

    if (atLeastOneFilled == true) {
        $('#submit').removeAttr("disabled");
    }
    else {
        $('#submit').attr("disabled", "disabled");
    }
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM