简体   繁体   English

Javascript:如何在所有字段都经过验证之前禁用提交按钮?

[英]Javascript: How to disable submit button until all fields are validated?

I have several validation functions that work fine and I want to write an additional validation in simple javascript ( no jQuery, etc ) for the entire form that disables/enables the Submit button depending on whether the other validation functions return true or false. 我有几个验证功能可以正常工作,我想用简单的javascript( 没有jQuery等 )编写整个窗体的附加验证,该窗体根据其他验证函数返回true还是false来禁用/启用Submit按钮。 How do I go about that? 我该怎么办?

For example, for my main HTML I have: 例如,对于我的主要HTML,我有:

 <form id="form" method="POST">
      <label class="form">Field 1</label><input type="text" name="input1" onkeyup="validateInput1(); return false">
      <label class="form">Field 2</label><input type="text" name="input2" onkeyup="validateInput2(); return false">
      ...
      <button id="submit" type="submit" value="submit">Submit</button>
 </form>

For my script I have: 对于我的脚本,我有:

 function validateInput1(){
 ...
 }

 function validateInput2(){
 ...
 }

Now I want to write a function with something like: 现在,我想编写一个类似以下内容的函数:

function validateForm(){

    var submitButton = document.getElementById('submit');
    submitButton.disabled = true;

    /*If all other validation functions like validateInput1() returns true then submitButton.disabled = false*/
}

How do I do this? 我该怎么做呢?

Start the button off as disabled. 将按钮启动为禁用状态。 Hook to the onchange event for each of the form inputs, and then have it check the validateForm() function to see if all the forms are valid. 挂钩到每个表单输入的onchange事件,然后让它检查validateForm()函数以查看所有表单是否有效。 After that, if they're all valid, set the submit button to enabled, otherwise set it to disabled. 之后,如果它们都有效,请将提交按钮设置为启用,否则将其设置为禁用。

 var inputs = document.querySelectorAll('#form input'); var validateInput1 = function() { return document.getElementById('input1').value != ''; } var validateInput2 = function() { return document.getElementById('input2').value != ''; } var validateForm = function() { if ( !validateInput1() ) { return false; } if ( !validateInput2() ) { return false; } return true; } for ( var i = 0, len = inputs.length; i < len; i++ ) { var checkValid = function() { document.getElementById('submit').disabled = !validateForm(); //Is the same as: /*if ( !validateForm() ) { document.getElementById('submitButton').disabled = true; } else { document.getElementById('submitButton').disabled = false; }*/ } inputs[i].addEventListener('change', checkValid); inputs[i].addEventListener('keyup', checkValid); } 
 <form id="form" method="POST" onsubmit="alert('Submitted!'); return false"> <label class="form">Field 1</label><input type="text" name="input1" id="input1"> <label class="form">Field 2</label><input type="text" name="input2" id="input2"> <button id="submit" type="submit" value="submit" disabled="disabled">Submit</button> </form> 

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

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