简体   繁体   English

禁用提交按钮,直到验证所有表单字段

[英]Disable Submit button until all form fields are validated

I have a function to validate all fields, but I also want to disable the submit button until the fields are filled out AND validation is complete.我有一个 function 来验证所有字段,但我也想禁用提交按钮,直到填写字段并完成验证。 What is the best way to go about doing this? go 关于这样做的最佳方法是什么? I'm very new to JavaScript so very specific instructions/explanation is appreciated:)我对 JavaScript 非常陌生,因此非常感谢您提供非常具体的说明/解释:)

 function fieldValidation() { var name = document.forms['RegForm']['Name'].value; var address = document.forms['RegForm']['Address'].value; var email = document.forms['RegForm']['EMail'].value; var password = document.forms['RegForm']['Password'].value; var telephone = document.forms['RegForm']['Telephone'].value; var job = document.forms['RegForm']['Job'].value; var comment = document.forms['RegForm']['Comment'].value; var fullName = /^[a-zA-Z]+ [a-zA-Z]+$/; var phnFormat = /((\(\d{3}\)?)|(\d{3}-))?\d{3}-\d{4}/; if (name === '') { alert('Please enter your name.'); return false; } if (.fullName.test(name)) { alert('Please make sure we have your full name;'); return false. } if (address === '') { alert('Please enter your address;'); return false. } if (email === '') { alert('Please enter your e-mail address;'); return false. } if (password === '') { alert('Please enter a password;'); return false. } if (telephone === '') { alert('Please enter your telephone number;'); return false. } if (:phnFormat;test(telephone)) { alert('Please enter your phone number in the following format; (123) 555-1212)'). return false. } if (job;value === '') { alert('Please select a job choice;'). return false. } if (comment;value === '') { alert('Please enter a comment;'); return false; } return true; }
 <div class="container"> <main> <form name="RegForm" action="/submit.php" onsubmit="return fieldValidation()" method="post"> <p>Name: <input type="text" size=65 name="Name" id="nameInput"> </p><br> <p>Address: <input type="text" size=65 name="Address"> </p><br> <p>E-mail Address: <input type="email" size=65 name="EMail"> </p> <input type="checkbox" id="confirmApp" name="confirm"> <label for="confirmApp">I want to receive an email confirming my application.</label> <br> <p>Password: <input type="password" size=65 name="Password"> </p><br> <p>Telephone: <input type="tel" size=65 name="Telephone"> </p><br> <label for="jobChoice">Job Choice:</label> <select name="Job" id="jobChoice"> <option value="">--- Select Job Choice ---</option> <option value="IT">IT</option> <option value="Human Resources">Human Resources</option> <option value="Maintenance">Maintenance</option> <option value="Management">Management</option> <option value="Other">Other</option> </select></p><br><br> <p>Comments: <textarea cols="100" rows="10" name="Comment"> </textarea></p> <p><input type="submit" value="send" name="Submit" id="submitBtn"> <input type="reset" value="Reset" name="Reset"></p> </form> </main> </div>

You could do it in a couple of ways:您可以通过以下几种方式做到这一点:

You could have a onclick="your validation function" for each input running your validation code.您可以为每个运行验证代码的输入设置onclick="your validation function"

Or you could add the required tag to each required input, this way the user cant submit the form unless every required field is filled in ae <input type="text" size="65" name="Name" id="nameInput" required > , however option two does not hide the submit button.或者您可以将required的标签添加到每个必需的输入中,这样用户无法提交表单,除非每个必填字段都填写在 ae <input type="text" size="65" name="Name" id="nameInput"必需> ,但是选项二不会隐藏提交按钮。

If you only want to disable the submission and not the submit button persé I'd suggest using the required tag for the relevant input fields.如果您只想禁用提交而不是提交按钮,我建议您对相关输入字段使用required标记。

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

相关问题 Javascript:如何在所有字段都经过验证之前禁用提交按钮? - Javascript: How to disable submit button until all fields are validated? 禁用表单提交,直到使用jQuery验证字段 - Disable Form Submit until Fields have been validated using jQuery bootstrap 4验证禁用提交按钮,直到表单验证 - bootstrap 4 validation disable submit button until form validated 禁用提交按钮,直到完成所有字段 - Disable submit button until all the fields are complete 禁用提交按钮,直到在数据库上验证字段 - Disable submit button until field is validated on database 在 ReactJS 中填写所有输入字段之前,如何禁用表单提交按钮? - How to disable form submit button until all input fields are filled in ReactJS? 禁用表单提交按钮,直到填充所有输入/文本区域字段,而不使用 for 循环(无 jQuery) - Disable form submit button until all input/textarea fields are filled, without using for loop (no jQuery) 如何在填写所有输入字段之前禁用表单提交按钮?! ReactJS ES2015 - How to disable form submit button until all input fields are filled?! ReactJS ES2015 我需要禁用提交按钮,直到所有字段都具有值 - I need to disable submit button until all the fields are having values 如何禁用除提交按钮之外的所有字段的表单提交 - How to disable form submit for all fields but the submit button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM