英文:
How to validate only parts of a form?
问题
我有一个多步表单,其中包含两个字段集,每个字段集中包含两个必填输入字段。我想在用户想要继续到第二个字段集时仅验证第一个字段集。
然而,根据我的当前方法,整个表单都会被验证,尽管我只在第一个字段集的输入元素上调用了checkValidity()
。结果是,当进入第二个字段集时,用户会看到无效的输入字段和警告工具提示。
我做错了什么?如何可以分别验证这两个字段集?我找到了关于jQuery和JSP的方法,但没有找到纯ES6的方法。
以下是我的代码。
let fieldsetElements;
function validateFieldset(step) {
const inputElements = document.querySelectorAll(`fieldset.active input`);
return !Array.from(inputElements).find((element) => !element.checkValidity());
}
function goToStep(step) {
Array.from(fieldsetElements)
.filter((element) => element.classList.contains("active"))
.forEach((element) => element.classList.remove("active"));
fieldsetElements[step - 1].classList.add("active");
}
function continueToStep(nextStep) {
const currentStep = nextStep - 1;
const fieldsetValid = validateFieldset(currentStep);
if (fieldsetValid) {
goToStep(nextStep);
}
}
document.addEventListener("DOMContentLoaded", () => {
fieldsetElements = document.getElementsByTagName("fieldset");
goToStep(1);
});
fieldset {
display: none;
}
fieldset.active {
display: block;
}
<form>
<fieldset>
<legend>Your name</legend>
<p>
<label for="prename">Prename (required)</label><br />
<input type="text" id="prename" name="prename" required />
</p>
<p>
<label for="surname">Surname (required)</label><br />
<input type="text" id="surname" name="surname" required />
</p>
<p>
<button onclick="continueToStep(2)">Continue</button>
</p>
</fieldset>
<fieldset>
<legend>Your contact information</legend>
<p>
<label for="email">Email (required)</label><br />
<input type="email" id="email" name="email" required />
</p>
<p>
<label for="phone">Phone</label><br />
<input type="tel" id="phone" name="phone" />
</p>
<button onclick="goToStep(1)">Go back</button>
<button type="submit">Submit</button>
</fieldset>
</form>
英文:
I have a multistep form with two fieldsets, containing two required inputs each. I want to validate only the first fieldset when the user wants to continue to the second one.
However, with my current approach, the entire form is validated, although I call checkValidity()
only on the input elements of the first fieldset. As a result, when progressing to the second fieldset, users are greeted with invalidated input fields and warning tooltips.
What am I doing wrong? How can I validate the fieldsets separately? I've found approaches for jQuery and JSP, but not for plain ES6.
Here is my code.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let fieldsetElements;
function validateFieldset(step) {
const inputElements = document.querySelectorAll(`fieldset.active input`);
return !Array.from(inputElements).find((element) => !element.checkValidity());
}
function goToStep(step) {
Array.from(fieldsetElements)
.filter((element) => element.classList.contains("active"))
.forEach((element) => element.classList.remove("active"));
fieldsetElements[step - 1].classList.add("active");
}
function continueToStep(nextStep) {
const currentStep = nextStep - 1;
const fieldsetValid = validateFieldset(currentStep);
if (fieldsetValid) {
goToStep(nextStep);
}
}
document.addEventListener("DOMContentLoaded", () => {
fieldsetElements = document.getElementsByTagName("fieldset");
goToStep(1);
});
<!-- language: lang-css -->
fieldset {
display: none;
}
fieldset.active {
display: block;
}
<!-- language: lang-html -->
<form>
<fieldset>
<legend>Your name</legend>
<p>
<label for="prename">Prename (required)</label><br />
<input type="text" id="prename" name="prename" required />
</p>
<p>
<label for="surname">Surname (required)</label><br />
<input type="text" id="surname" name="surname" required />
</p>
<p>
<button onclick="continueToStep(2)">Continue</button>
</p>
</fieldset>
<fieldset>
<legend>Your contact information</legend>
<p>
<label for="email">Email (required)</label><br />
<input type="email" id="email" name="email" required />
</p>
<p>
<label for="phone">Phone</label><br />
<input type="tel" id="phone" name="phone" />
</p>
<button onclick="goToStep(1)">Go back</button>
<button type="submit">Submit</button>
</fieldset>
</form>
<!-- end snippet -->
答案1
得分: 1
问题在于没有type
属性,所有的button
标签都会触发表单验证。请将type="button"
添加到你的按钮上,它就会按你的期望工作。
<button type="button" onclick="continueToStep(2)">Continue</button>
<button type="button" onclick="goToStep(1)">Go back</button>
<button type="submit">Submit</button>
请注意,我只翻译了代码部分,没有包含其他内容。
英文:
The issue is that without a type
attribute all button
tags trigger form validation. Add type="button"
to your buttons and it should work as you expect.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let fieldsetElements;
function validateFieldset(step) {
const inputElements = document.querySelectorAll(`fieldset.active input`);
return !Array.from(inputElements).find((element) => !element.checkValidity());
}
function goToStep(step) {
Array.from(fieldsetElements)
.filter((element) => element.classList.contains("active"))
.forEach((element) => element.classList.remove("active"));
fieldsetElements[step - 1].classList.add("active");
}
function continueToStep(nextStep) {
const currentStep = nextStep - 1;
const fieldsetValid = validateFieldset(currentStep);
if (fieldsetValid) {
goToStep(nextStep);
}
}
document.addEventListener("DOMContentLoaded", () => {
fieldsetElements = document.getElementsByTagName("fieldset");
goToStep(1);
});
<!-- language: lang-css -->
fieldset {
display: none;
}
fieldset.active {
display: block;
}
<!-- language: lang-html -->
<form>
<fieldset>
<legend>Your name</legend>
<p>
<label for="prename">Prename (required)</label><br />
<input type="text" id="prename" name="prename" required />
</p>
<p>
<label for="surname">Surname (required)</label><br />
<input type="text" id="surname" name="surname" required />
</p>
<p>
<button type="button" onclick="continueToStep(2)">Continue</button>
</p>
</fieldset>
<fieldset>
<legend>Your contact information</legend>
<p>
<label for="email">Email (required)</label><br />
<input type="email" id="email" name="email" required />
</p>
<p>
<label for="phone">Phone</label><br />
<input type="tel" id="phone" name="phone" />
</p>
<button type="button" onclick="goToStep(1)">Go back</button>
<button type="submit">Submit</button>
</fieldset>
</form>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论