I have created this very simple code just for my practice. I am trying to test the jQuery for the required fields. But it is not working. Can someone tell me why? Also, it would be really helpful if you can also tell how can I improve it. I was thinking to make it even better by displaying error message below the field instead of the alert box and also to change the text field colour to red if an error occurred.
$(document).ready(function() {
$('.btn').click(function() {
if ($('#fn').val() == "" || $('#ln').val() == "") {
alert('Please complete the required field(s)');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body style="background-color : black">
<br /><br /><br />
<div style="text-align:center; color:white;">
<form>
<strong>
First Name         <input id = "fn" type = "text" /> <br /><br />
Middle Name         <input id = "ln" type = "text" /> <br /><br />
Last Name         <input type = "text" /> <br /><br />
Gender         <select>
<option value = "Male">Male</option>
<option value = "Female">Female</option>
</select> <br /><br />
Married         <select>
<option value = "No">No</option>
<option value = "Yes">Yes</option>
</select> <br /><br />
Spouse Name         <input type = "text" /> <br /><br />
DOB         <input type = "date" /> <br /><br />
Email         <input type = "text" /> <br /><br />
Age         <input type = "text" /> <br /><br />
Mobile         <input type = "text" /> <br /><br /><br />
<button class="btn"> SUBMIT </button>
</strong>
</form>
</div>