当我有 5 个字段时,如何使用 JavaScript 验证表单中的单个字段

how to validate single field in form when i have 5 fields using javascript?

本文关键字:字段 验证 JavaScript 表单 单个 何使用      更新时间:2023-09-26

>我有一个有四个字段的表单,当所有字段都为空并单击提交按钮时 - 它应该提醒"请至少选择一个值"。

当我在任何字段中输入值时,表单应该提交。我尝试了以下代码,但它不起作用

function validatefleid() {
  var computername = document.myform.computername.value;
  var monitorname = document.myform.monitorname.value;
  var tvname = document.myform.tvname.value;
  var laptopname = document.myform.laptopname.value;
  var cellphonename = document.myform.cellphonename.value;
  if ((computername == null || computername == "") || (monitorname == null || monitorname == "") || (tvname == null || tvname == "") || (laptopname == null || laptopname == "") || (cellphonename == null || cellphonename == "")) {
    alert("Please atleast one value");
    return false;
  }
}
<form name="myform" onsubmit="return validatefleid()">
  computer
  <input type="text" class="form-control inpt-bx-txtclr-home" name="computername" id="computerid" placeholder="000">monitor
  <input type="text" class="form-control inpt-bx-txtclr-home" name="monitorname" id="monitorid" placeholder="000">tv
  <input type="text" class="form-control inpt-bx-txtclr-home" name="tvname" id="tvid" placeholder="000">laptop
  <input type="text" class="form-control inpt-bx-txtclr-home" name="laptopname" id="laptopid" placeholder="000">cellphone
  <input type="text" class="form-control inpt-bx-txtclr-home" name="cellphonename" id="cellphoneid" placeholder="000">
  <button type="submit">Calculate</button>
</form>

可以通过遍历表单中的所有input,并查找至少一个值与''不同的input。检查演示:

$(function(){
  var $form = $('#myform');
  $form.submit(function(){
    var valid = false;
    $('input', $form).each(function(){
      if($(this).val() != ''){
        valid = true;
      }
    });
    if (!valid) {
      alert("Please atleast one value");
    }
    return valid;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myform" name="myform">
computer<input type="text" class="form-control inpt-bx-txtclr-home" name="computername" id="computerid" placeholder="000">
monitor<input type="text" class="form-control inpt-bx-txtclr-home" name="monitorname" id="monitorid" placeholder="000">
tv<input type="text" class="form-control inpt-bx-txtclr-home" name="tvname" id="tvid" placeholder="000">
laptop<input type="text" class="form-control inpt-bx-txtclr-home" name="laptopname" id="laptopid" placeholder="000">
cellphone<input type="text" class="form-control inpt-bx-txtclr-home" name="cellphonename" id="cellphoneid" placeholder="000">
<button  type="submit" >Calculate</button>
</form>

Without jQuery :

https://jsfiddle.net/uL28jsf9/

<script type="text/javascript">
function validatefleid()
{
    var allFieldsNames = ['computername', 'monitorname', 'tvname', 'laptopname', 'cellphonename'];
    var checkIfFieldEmpty function(fieldName) {
      var fieldValue = document.myform[fieldName].value;
      return fieldValue === "" || fieldValue === null
    }
  var allFieldsEmpty = allFieldsNames.every(checkIfFieldEmpty);
  if (allFieldsEmpty) {
    alert("Please atleast one value");
    return false;
  }
  else {
    alert("I'm ok with that !")
  }
}
</script>
oops i have got the answer 
I have a form which as four fields when the all the fields are empty and submit button is clicked it should alert as "please select atleast one value" when i entry the value in any of the one fields the form should get submitted i have tries the following code but its not working  

     <script type="text/javascript">
        function validatefleid()
        {
     var computername=document.myform.computername.value;
     var monitorname=document.myform.monitorname.value;
     var tvname=document.myform.tvname.value;
     var laptopname=document.myform.laptopname.value;
     var cellphonename=document.myform.cellphonename.value;
     if((computername==null || computername=="") && (monitorname==null || monitorname=="") && (tvname==null || tvname=="") && (laptopname==null || laptopname=="") && (cellphonename==null || cellphonename==""))
     {
        alert("Please atleast one value");
        return false;
     }
        }

    </script>
    <form name="myform" onsubmit="return validatefleid()">

    computer<input type="text" class="form-control inpt-bx-txtclr-home" name="computername" id="computerid" placeholder="000">
    monitor<input type="text" class="form-control inpt-bx-txtclr-home" name="monitorname" id="monitorid" placeholder="000">
    tv<input type="text" class="form-control inpt-bx-txtclr-home" name="tvname" id="tvid" placeholder="000">
    laptop<input type="text" class="form-control inpt-bx-txtclr-home" name="laptopname" id="laptopid" placeholder="000">
    cellphone<input type="text" class="form-control inpt-bx-txtclr-home" name="cellphonename" id="cellphoneid" placeholder="000">
    <button  type="submit" >Calculate</button>
    </form>

这里是Validatefleid函数的完整跨浏览器工作版本。

function validatefleid(){
    var inputIds = ['computerid','monitorid','tvid','laptopid','cellphoneid'];
    var hasEnteredAnyValue = false;
    for(var i=0;i<inputIds.length;i++){
        hasEnteredAnyValue = document.getElementById(inputIds[i]).value;
        if(hasEnteredAnyValue)break;        
    }
     if(!hasEnteredAnyValue)
     {
        alert("Please atleast one value");
        return false;
     }
}