我有一个类似于以下代码的 html 表单。我的问题是我想根据多个条件进行验证...数字可以是 0 或必须大于 700。我使用数组作为输入名称,这使我很难获得 javascript 解决方案在职的。在这种情况下,如何使用多个标准进行验证?
<form name="endingweights" method="post">
<input type="hidden" value="1" name="strain[]"></td><td><input type="number" step="any" name="ending[]" min="0" max="1500" required />
<input type="hidden" value="2" name="strain[]"></td><td><input type="number" step="any" name="ending[]" min="0" max="1500" required />
<input type="hidden" value="3" name="strain[]"></td><td><input type="number" step="any" name="ending[]" min="0" max="1500" required />
最佳答案
使用以下 JavaScript 函数对其进行验证。
<script>
function validateForm() {
var x = document.forms["endingweights"].elements["ending[]"];
for (var i = 0; i < x.length; i++) {
var aControl = x[i].value;
if (aControl>0 && aControl<700) {
alert("Incorrect Weight, must be 0 or greater than 700.");
x[i].focus();
return false;
}
}}
</script>
关于javascript - html表单上的多重验证规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26979565/