[英]Text Box Validation with javascript
有文本框a,如果它已填寫,請確保在提交時也填寫文本框b,反之亦然,如果填寫框b,請確保a中也有數據。 是否可以循環檢查許多文本框,如果文本框的行 a 有數據檢查第二行文本框也有數據在行 a 中。 a行中的所有文本框都不需要填寫。謝謝。
你的問題有點難以理解; 如果你能更好地解釋你真正想要的東西,那將會很有幫助。 你不需要任何條件; 只需執行以下操作:
<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>
如果您使用數組來保存對文本框的引用,這應該很簡單。 例如(在我的腦海中,所以這不會是 100% 正確),假設你有 5 個橫切和 3 個向下:
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;
您現在可以遍歷row[0]
中的數組,如果您發現,例如, row[0][2]
有文本,您只需要驗證row[1][2]
也有文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.