0

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. All the text boxes in row a don't need to be filled in. Thanks.

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Whoops... fixed it I think. (Too much Qt!)
Ok, but every time someone does that I have to search the damn HTML5 spec to see if it's some new method of an obscure API or not. :-)
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.