Below is my code for a simple jQuery validation. However I want the validation to appear if the field is left empty and the add task button is clicked. At the minute it appears all the time. This form is to add a to do item to a list so will be blank once loaded
$(document).ready(function() {
$('#AddTask').on('click', function() {
if ($('#title').val() == '') {
$(this).next('.errorMsg').show();
} else {
$(this).next('.errorMsg').hide();
}
});
});
$(document).ready(function() {
$("#todo").hide(); //hide form until create new task button is clicked
});
$(document).ready(function() {
$("#new").on('click', function() {
$("#todo").toggle();
$("#new").hide();
});
});
$(document).ready(function() {
$("#AddTask").click(function() {
$("#todo").slideUp("slow", function() {
// Animation complete.
});
$("#new").show();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label for="exampleFormControlInput1">Title of Task</label>
<input type="text" class="form-control" id="title" placeholder="Title">
<span class="errorMsg">Title of task required </span>
<button type="required" id="AddTask">Add Task</button>