Validations using jQuery

<form action ="">
<!-- First Author -->
Author Name: <input type="text" id="AuthorName1" name="authorNAMEinput">
<img src="error.png" id="AuthorName1_error" style="display:none">
Author DOB: <input type="text" id="AuthorDOB1" name="authorDOBinput">
Author Company: <input type="text" id="AuthorCompany1" name="authorCompanyinput">
<img src="error.png" id="AuthorCompany1_error" style="display:none">
<!-- Second Author -->
Author Name: <input type="text" id="AuthorName2" name="authorNAMEinput">
<img src="error.png" id="AuthorName2_error" style="display:none">
Author DOB: <input type="text" id="AuthorDOB2" name="authorDOBinput">
Author Company: <input type="text" id="AuthorCompany2" name="authorCompanyinput">
<img src="error.png" id="AuthorCompany2_error" style="display:none">
<!-- Third Author snipped -->
<input type ="button" id="authorbutton" name="authorbuttoninput">
</form>

<script>
$('#authorbutton').click(function() {
var valid = true;
var requiredFields = ['AuthorName1','AuthorCompany1','AuthorName2','AuthorCompany2'];
for(var i = 0; i < requiredFields.length; i++) {
var val = $('#'+requiredFields[i]));
if (!val) {
$('#'+requiredFields[i]+'_error').show();
valid = false;
} else {
$('#'+requiredFields[i]+'_error').hide();
}
}
if (valid) { // ajax stuff }
});
</script>

Comments