This idea came to me as a hunch. At my job I have to do alot validation of forms and I get tired to write the entire document.form.name.value and other forms of checking (like selected, etc...) so I thought "Hmmmm, can U use arrays??" and lo-and-behold here it is...
This works in IE and NS6 (not NS4, for some reason...it doesn't like the for loop)...you can use textboxes, select choices and radiobuttons, etc...all you have to do is make sure that all the form elements have the same id attribute name...
This is an example code...enjoy!!
<html>
<head>
<title>Validation via Arrays</title>
[color red]
<script>
function GUJUm0deL() { //input the msg message here...just increment by one for more alerts...
var msg1 = "please answer 1";
var msg2 = "please answer 2";
var msg3 = "please answer 3";
var msg4 = "please answer 4";
var msg5 = "please answer 5";
//make sure the id=ONE is the same in all checks (id=ONE is defined in the <form> next to each form element...
for(i=0; i<=document.form1.ONE.length; i++) {
if(document.form1.ONE[0].value == "") {
alert(msg1);
return false;
}
if(document.form1.ONE[1].value == "") {
alert(msg2);
return false;
}
if(document.form1.ONE[2].value == "") {
alert(msg3);
return false;
}
if(document.form1.ONE[3].value == "") {
alert(msg4);
return false;
}
if(document.form1.ONE[4].options[0].selected == true) {
alert(msg5);
return false;
}
}
}
</script>[/color]
</head>
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.