【问题标题】:Javascript for PDF form validation用于 PDF 表单验证的 Javascript
【发布时间】:2021-09-07 18:18:17
【问题描述】:
代码运行良好。我想添加一个选项,从 7 个复选框中选择至少一个。我不知道该怎么做。
var emptyFields = [];
for (var i = 0; i < this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));
if (f.type != "button" && f.required) {
if ((f.type == "text" && f.value == "") || (f.type == "checkbox" && f.value == "Off") || (f.type == "signature" && f.value == "")) emptyFields.push(f.name);
}
}
if (emptyFields.length > 0) {
app.alert("Error! Please fill all red highlighted fields")
} else {
print();
}
【问题讨论】:
-
欢迎来到 SO!我建议所有新用户访问How to Ask,以获取有关如何以最能让社区提供帮助的方式提出问题的提示。 “我想添加一个选项,从 7 个复选框中选择至少一个。我不知道该怎么做。” 我不确定这意味着什么——如果用户有七个复选框,那么根据复选框的性质,他们可以选择其中的一个或多个……您能否提供minimal reproducible example 和您的具体、明确的要求,以及您迄今为止尝试实施的方式?祝你好运,编码愉快!
标签:
javascript
forms
validation
【解决方案1】:
你只需要改变你那里的逻辑:
- 创建一个新数组
checkedCheckboxes。
- 在其中存储
f.type === "checkbox" && f.checked === true 所在的元素。
- 最后,检查
checkedCheckboxes.length === 0 是否显示适当的错误消息,就像您对 emptyFields 所做的那样。
编辑:工作示例:
// Get a reference to the error box:
const errorBox = document.getElementById('errorBox');
// To the validate button:
const validateButton = document.getElementById('validateButton');
// And to all input elements:
const inputs = document.querySelectorAll('input');
function validateForm() {
// Reset the error box:
errorBox.innerHTML = '';
// Here we'll keep track of empty text inputs:
const emptyFields = [];
// And here we'll keep track of how many selected checkboxes each
// group has:
const checkedCheckboxesByGroup = {
checkboxes: 0,
checkboxes2: 0,
};
Array.from(inputs).map((input) => {
if (input.type === "text" && input.value === "") {
// Keep track of empty inputs:
emptyFields.push(input);
}
if (input.type === "checkbox" && input.checked) {
// Get the checkbox group (name attribute without the
// square brackets):
const groupName = input.name.slice(0, -2);
// Increment this counter:
++checkedCheckboxesByGroup[groupName];
}
});
if (emptyFields.length > 0) {
errorBox.innerHTML += "<p>All fields are required.</p>";
}
Object.entries(checkedCheckboxesByGroup).map(([groupName, checkedCheckboxes]) => {
if (checkedCheckboxes === 0)
errorBox.innerHTML += `<p>You need to select at least one element in ${ groupName }.</p>`;
});
}
// Validate the form every time we click the button:
validateButton.addEventListener('click', validateForm);
body,
input,
button {
font-family: monospace;
}
p {
margin: 0;
}
.label {
display: block;
border: 0;
margin: 0 0 16px;
padding: 0;
}
.labelTitle {
display: block;
margin-bottom: 4px;
}
input[type="text"],
button {
border: 2px solid black;
padding: 8px;
border-radius: 2px;
background: white;
}
.checkboxLabel {
display: block;
position: relative;
padding: 8px 8px 8px 32px;
}
input[type="checkbox"] {
position: absolute;
top: 50%;
left: 16px;
transform: translate(-50%, -50%);
margin: 0;
}
#errorBox {
border: 2px solid red;
padding: 8px;
border-radius: 2px;
margin-bottom: 16px;
}
#errorBox > p + p {
margin-top: 16px;
}
#errorBox:empty {
display: none;
}
<label class="label">
<span class="labelTitle">Field 1</span>
<input type="text" placeholder="Field 1" />
</label>
<label class="label">
<span class="labelTitle">Field 1</span>
<input type="text" placeholder="Field 1" />
</label>
<label class="label">
<span class="labelTitle">Field 1</span>
<input type="text" placeholder="Field 1" />
</label>
<fieldset class="label">
<span class="labelTitle">Checkboxes Group 1</span>
<label class="checkboxLabel"><input type="checkbox" value="1" name="checkboxes[]" />Checkbox 1</label>
<label class="checkboxLabel"><input type="checkbox" value="2" name="checkboxes[]" />Checkbox 2</label>
<label class="checkboxLabel"><input type="checkbox" value="3" name="checkboxes[]" />Checkbox 3</label>
<label class="checkboxLabel"><input type="checkbox" value="4" name="checkboxes[]" />Checkbox 4</label>
<label class="checkboxLabel"><input type="checkbox" value="5" name="checkboxes[]" />Checkbox 5</label>
<label class="checkboxLabel"><input type="checkbox" value="6" name="checkboxes[]" />Checkbox 6</label>
<label class="checkboxLabel"><input type="checkbox" value="7" name="checkboxes[]" />Checkbox 7</label>
</fieldset>
<fieldset class="label">
<span class="labelTitle">Checkboxes Group 2</span>
<label class="checkboxLabel"><input type="checkbox" value="10" name="checkboxes2[]" />Checkbox 10</label>
<label class="checkboxLabel"><input type="checkbox" value="20" name="checkboxes2[]" />Checkbox 20</label>
<label class="checkboxLabel"><input type="checkbox" value="30" name="checkboxes2[]" />Checkbox 30</label>
<label class="checkboxLabel"><input type="checkbox" value="40" name="checkboxes2[]" />Checkbox 40</label>
<label class="checkboxLabel"><input type="checkbox" value="50" name="checkboxes2[]" />Checkbox 50</label>
<label class="checkboxLabel"><input type="checkbox" value="60" name="checkboxes2[]" />Checkbox 60</label>
<label class="checkboxLabel"><input type="checkbox" value="70" name="checkboxes2[]" />Checkbox 70</label>
</fieldset>
<div id="errorBox"></div>
<button id="validateButton">Validate Form</button>