i have a form that when submitted, if any field is empty id like to prevent the submission and add a class to the field.
For some reason I cant seem to get it too work, Ive added a fiddle, can anybody point out where im goign wrong?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are a couple of problems with your fiddle. Firstly, you haven’t closed the
readyevent handler. Secondly, are passing$thisinto jQuery which isundefined. You need to passthisinstead.Finally, the form is always going to be submitted because you have to actually stop the submission. You can call the
preventDefaultmethod of the event object to do so. Here’s a new version of your code:Also note that it’s unnecessary to use
$(this).val()inside theeachloop.thiswill be a reference to a DOM element, and that element will have avalueproperty, so it’s more efficient to simply usethis.value.