Required attribute in multi step form

拜拜、爱过 提交于 2021-01-29 12:39:46

问题


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.


回答1:


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 !



来源:https://stackoverflow.com/questions/52134980/required-attribute-in-multi-step-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!