开发者

Text Box Validation with javascript

Have text box a and if it is filled in make sure text box b is filled in also when doing a submit and vice a versa if box b is filled in make sure a has data also in it. Can this be looped to check many text boxes that if the row a of text boxes has data check 2nd row of text boxes also have data in the ones that row a has. A开发者_开发问答ll the text boxes in row a don't need to be filled in. Thanks.


Your question is a bit hard to understand; it'd be helpful if you could better explain what you actually want. You don't need to have any conditions; just do the following:

<script>
function checkForm() {
    if (!isEmpty(document.myForm.checkA.value)
        && !isEmpty(document.myForm.checkB.value))
        return true;
    else
        return false;
}

function isEmpty(text) {
    return text.length == 0 || !text.match(/[^\s]/))
}

</script>

<form name="myForm" onSubmit="return checkForm();">
    <input name="textA" type="text" />
    <input name="textB" type="text" />
    <input type="submit" value="Submit!" />
</form>


This should be straightforward if you use an array to hold references to your text boxes. For example (off the top of my head so this is not going to be 100% right), let's say you have 5 across and 3 down:

var col = new Array(5);
var row = new Array(3);
col[0] = document.myForm.checkA1;
col[1] = document.myForm.checkB1;
// etc

row[0] = col;

col = new Array(5);
col[0] = document.myForm.checkA2;
col[1] = document.myForm.checkB2;
// etc

row[1] = col;

col = new Array(5);
col[0] = document.myForm.checkA3;
col[1] = document.myForm.checkB3;
// etc

row[2] = col;

You can now loop over the array in row[0] and if you find, e.g., that row[0][2] has text you just need to verify that row[1][2] also has text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜