Before submitting the form, I need to first validate the data that is being input by the user on key up. Let’s say I have an input like this:
<input type="text" value="" name="title" class="input" id="title" placeholder="Job Title">
On my script I need to validate the input of the user on key up:
$("#title").on("keyup", function(e) {
if($(this).val() == "") {
$("#errorMsg").html('This field is required');
return false;
} else {
$("#errorMsg").html('');
return true;
}
});
After the input has been validated successfully, the user can now submit the form, however if the input is not validated successfully and the user clicks the submit button, I need to have an error message saying "fix your errors first";
$('#save_btn').on('click', function(e){
e.preventDefault();
if ( user input is false ){
alert('Fix your errors first!');
} else{
alert('Validation Successful')
}
I will not include the ajax part of my code cause it’s already working fine. I just need this submit validation to get fixed. I’m thinking it has something to do with functions (?) but I don’t know how to execute it.
2
Answers
Or you can use jquery validation plugin https://jqueryvalidation.org/documentation/