I've tried the following code for javascript validation. When the form is submitted, only the first input field is validated. How could I validate all the dynamic values of the form?
<script type="text/javascript">
function validate() {
if (document.f.phone.value.length == "0") {
alert("Phone is required");
document.f.phone.focus();
return false;
} else if (document.f.file.value.length == "0") {
alert("file is required ");
document.f.file.focus();
return false;
}
return true;
}
</script>
<?php
echo '<form name="f" action="" method="post" enctype="multipart/form-data" onsubmit="return validate();">
<input type="text" name="phone[]" class="required">
<input type="file" name="file[]" class="required">
<input type="button" class="add" value="add"/>
<input type="submit" value="Submit" name="submit" class="Submit">
';
?>
document.f.file.filesinstead ofdocument.f.file.value.file[]andphone[]you need to access them likedocument.f.elements['phone[]'].valueanddocument.f.elements['file[]'].value