html - Required attribute in multi step form
问题描述
I have used a multistep form and I am using "required" attribute in some of the input fields. As such
<input type="text" placeholder="Full name" id="name" name="name" required="Please enter your full name">
The user fills one form and clicks next and after three nexts he can submit.
But the required attribute triggers only when the I click the submit button.I want the user to fill all the fields before he clicks next on the form. By that I mean that the required must trigger on the next button.
解决方案
You can run an function to check if the input field is empty, then add the required attribute to the input element.
Example code:
function checkValue() {
var name = document.getElementById("name");
if(name.value === "") {
var att = document.createAttribute("required");
name.setAttributeNode(att);
}
}
<form>
<input type="text" placeholder="Full name" id="name" name="name">
<button onclick = "checkValue()">Next</button>
</form>
Hope this helps !
推荐阅读
- wget - 如何导出使用 Dotcms 制作的网站?
- javascript - 如何迭代数组(过滤器)?
- c# - 使用 Microsoft Graph API 的解决方法 NotFound?
- javascript - 在线将 ES6 javascript 文件转换为 ES5
- javascript - 当托管服务提供商需要 app.js 文件时,在 Phusion Passenger 上托管 NestJS 应用程序
- python - 无法打开 Docker 安装的文件夹/文件
- python - Ubuntu 服务器上的无头 Selenium
- c# - IAsyncResult 到 Byte[] 的转换
- typescript - vue3 typescript 问题,如何修复 ts(2305) 错误?
- javascript - 尝试使用 axios 和 formData 发布数据,但它是空的