如何验证条目的特定数量的文本输入字段?

时间:2014-11-12 12:54:45

标签: javascript validation

number_people_household是一个下拉列表,用于决定下一个问题中可见的输入文本字段数。当我想验证特定数量的可见字段时,我的问题就出现了。例如,如果有3个字段,我想验证文本的三个可见字段。如果一个或多个为空alert("mistake")。如果填写了所有可见字段,请执行loadNew()函数。

function checkSeveralFilled() {

    var e = document.getElementById("number_people_household");
    var amount = e.options[e.selectedIndex].text;

    var inp = document.getElementsByTagName('input');
    for(var i in inp; i < amount;){
        if(inp[i].type == "text"){
            if(inp[i].value != ""){
                loadNew();
            }
            else {
                alert("missing value");
            }
        }
    }
}

更新:

添加了一些HTML。出于某种原因,我的功能是没有识别任何输入类型等于&#34; text&#34;我假设。

<div class="question">
<div class="white_box">
    List the ages of people in your household?
</div>
<div class="answer">
    <div class="rule" id="rules">
        Only Numbers Allowed, eg 14, 15, 44, 45
    </div>

    <input id="person1" class="person1" type="text" placeholder="Person 1" onkeyup="checkInputs(this)"/>
    <input id="person2" class="person1" type="text" placeholder="Person 2" onkeyup="checkInputs(this)"/>
    <input id="person3" class="person1" type="text" placeholder="Person 3" onkeyup="checkInputs(this)"/>
    <input id="person4" class="person1" type="text" placeholder="Person 4" onkeyup="checkInputs(this)"/>
    <input id="person5" class="person1" type="text" placeholder="Person 5" onkeyup="checkInputs(this)"/>
    <input id="person6" class="person1" type="text" placeholder="Person 6" onkeyup="checkInputs(this)"/>
    <input id="person7" class="person1" type="text" placeholder="Person 7" onkeyup="checkInputs(this)"/>
    <input id="person8" class="person1" type="text" placeholder="Person 8" onkeyup="checkInputs(this)"/>
    <input id="person9" class="person1" type="text" placeholder="Person 9" onkeyup="checkInputs(this)"/>
    <input id="person10" class="person1" type="text" placeholder="Person 10" onkeyup="checkInputs(this)"/>
</div>
</div>

function checkSeveralFilled() {
debugger;
var a = document.getElementById("number_people_household");
var amount1 = a.options[a.selectedIndex].text;

var inp = document.getElementsByTagName('input');
var callFunc = true;

for (var i = 0; i < amount1; i++) {
    if(inp[i].type == "text"){
    alert("found");
        if(inp[i].value == ""){
            alert("missing value");
            inp[i].focus();
            callFunc = false;
            break;
        }
    }
}

if (callFunc)
{
    alert("call loadNew()");
}
}

0 个答案:

没有答案