我想通過表單字段進行循環,並根據存儲在數組中的正則表達式進行驗證。看不出這裏有什麼問題。請有任何想法嗎?訪問存儲表單驗證的正則表達式的2維數組
<script type="text/javascript">//<![CDATA[
//Adds my event listener so that page loads before form becomes operational
if(window.addEventListener){
window.addEventListener("load",initVal,false);
}else if (window.attachEvent){
window.attachEvent("onload",initVal);
} else { window.onload=initVal;}
function initVal() {
document.orderform.postcode.onchange=checkAll;
document.orderform.email.onchange=checkAll;
}
function checkAll(){
//create 2-dimensional array
frmflds= new Array (2);
for (i=0;i<frmflds.length;i++) {
frmflds [i] = new Array (2);
//populate arrays
frmflds[0][0] = 'postcode';
frmflds[0][1] = '/^[A-z][A-z]\d{2}\s\d{2}[A-z][A-z]|[A-z][A-z]\d{1}\s\d{1}[A-z][A-z]$/';
frmflds[1][0] = 'email';
frmflds[1][1] = '/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/'; }
// loop through form elements and validate against regex stored in array
for(var j = 0; j < frmflds.length; j++) {
var e = frmflds[j][0][1];
var OK= (e.exec(document.orderform.getElementById()[j].name.value);
if !OK
{
document.orderform.name[j].style.borderColor="red";
document.orderform.name[j].style.backgroundColor="pink";
document.orderform.Submit.onclick.disabled=true;
alert("Please correct any highlighted fields and submit again!");
}
else{
document.orderform.name[j].style.borderColor="Green";
document.orderform.name[j].style.backgroundColor="PaleGreen";
document.orderform.Submit.onclick.disabled=false;
}
} }
//]]></script>
</head>
<body>
<form method="post" action=""" name="orderform" id="orderfm">
<p><input type="text" name="postcode" id="postcode" ></p>
<p><input type="text" name="email" id="email" ></p>
<p><input label="" type="submit" name="Submit" value="Submit" id="Submit" disabled="disabled">
</form>
</body>
</html>