I'd like to use an array to move through specific DOM elements to check form values on submit. Logically, I think it would look like this:
Code: Select all
<script language="javascript">
function checkform( form ) {
//name,email,address all have values in the passed object...
//ie, form.name.value contains the correct string
var required_fields = Array('name','email','address');
for (i=0; i<required_fields.length; i++) { // loop goes from 0-2
if( form.required_fields[i].value == "" ) { //<--error happening here
alert('Please enter a valid ' + form.required_fields[i].value);
return false;
}
}
return true;
}
</script>
Thanks.