如何检查形式至少有一个字段填写(How to check that a form has AT LE

2019-07-31 03:49发布

我在建立一个网站的最后阶段,有一个关键的任务留给:要设置的是阻止用户能够提交一份空白搜索某种形式的验证。

我知道下面的帖子,但发现这个不明如何得到它的工作,以及如何用这个下拉的( jQuery验证-需要一个组中至少一个字段来填充 )

我如何能做到这一点任何想法,或者一个指南,可以帮助任何指针,将大规模升值呢?

Answer 1:

下面是应与所有表单字段类型的工作功能: textselectradiocheckboxtextareafile和HTML5输入类型,如email 。 此功能使得唯一的假设是,所有select的元素有一个optionvalue=""

/**
 * 1) gather all checkboxes and radio buttons
 * 2) gather all inputs that are not checkboxes or radios, and are not buttons (submit/button/reset)
 * 3) get only those checkboxes and radio buttons that are checked
 * 4) get only those field elements that have a value (spaces get trimmed)
 * 5) if the length of both resulting collections is zero, nothing has been filled out
 */
function checkFields(form) {
    var checks_radios = form.find(':checkbox, :radio'),
        inputs = form.find(':input').not(checks_radios).not('[type="submit"],[type="button"],[type="reset"]'),
        checked = checks_radios.filter(':checked'),
        filled = inputs.filter(function(){
            return $.trim($(this).val()).length > 0;
        });

    if(checked.length + filled.length === 0) {
        return false;
    }

    return true;
}

$(function(){
    $('#myForm').on('submit',function(){
        var oneFilled = checkFields($(this));
        // oneFilled === true if at least one field has a value
    });
});​

这里是一个演示:

---的jsfiddle DEMO ---



Answer 2:

我不是专家,但是这对我的作品。 我有一个选择栏和捐款的输入字段。 如果该量不上选择选项,则用户可以添加的量到输入字段中。 因此,在至少一个具有提交。

    function validateForm() {  
if (  myform.mySelectfield.value == "" 
    && myform.myInputfield2.value == "" 
    ) {
alert( "Please enter at least field" );
     return false;    
     }   
     } 
//--> 

当然,输入字段必须是数字所以我说:

 function validateForm2() {

VAR X = document.forms [ “myForm会”] [ “myInputfield2”]值。

if (/[^0-9]+$/.test(x))
    {
    alert("Please enter a numerical amount without a decimal point");
    myform.myInputfield2.focus();
    return false;
  } 

}

数值检查叫做的onkeyup:

onkeyup="validateForm2()"

我不得不做出第二功能validateForm2(),因为数值检查没有在与第一个工作。



Answer 3:

//select all text inputs in form and filter them based on...
var isFilled = $('input[type="text"]', 'form').filter(function() {
    return $.trim(this.value).length;  //text inputs have a value
}).length;                             //get the selector length

//if selector contains at least one filled in text input, submit form
if (isFilled) {..submit form..} 


Answer 4:

var filled = $('.property-list input[type="text"]').filter(function() {
    var v = $(this).val();
    // sanitize it first
    v = v.replace(/[\s]{2,}/g,' ').replace(/^\s*(.*?)\s*$/,'$1');
    $(this).val(v);
    // if you want it to contain min.
    // 3 consecutive alphanumeric chars, for example...
    return /[a-z0-9]{3,}/i.test(v);
}).get();
if (filled.length) {
    // do whatever with the result
    alert('all okay');
} else {
    $('.property-list input[type="text"]').focus().blur();
    // this is for the placeholders to refresh, at least in Safari...
    $('.property-list input[type="text"][value!=""]:first').focus();
    var msg = 'plase fill at least one field with min. 3 ';
    msg += 'consecutive alphanumeric characters';
    alert(msg);
}


文章来源: How to check that a form has AT LEAST one field filled in