I wrote a code to validate a form on client-side. Since I binded all the error messages on('input', function())
now the last case to take in consideration is when the user didn't even hit a required input leaving it empty.
If all the inputs in the form were required I could have used something like
$('#subButton').on('click', function(e) {
if (!$('#formName').val()) {
e.preventDefault();
alert("Fill all the required fields");
});
But since in my form there are required inputs (with class="req"
) and non required inputs, I would like to know if there's a method to perform the check only on the .req
inputs.
Something like:
$('#subButton').on('click', function(e) {
if (!$('#formName.req').val()) {
e.preventDefault();
alert("Fill all the required fields");
}
});
In other words I would like to perform the identical check which the up-to-date browsers do if the HTML required
option is specified, just to be sure that, if the browser is a bit old and doesn't "read" the required
option, jQuery prevents the form to be sent.