jQuery Form Validation before Ajax submit

You could use the submitHandler option. Basically put the $.ajax call inside this handler, i.e. invert it with the validation setup logic. $(‘#form’).validate({ … your validation rules come here, submitHandler: function(form) { $.ajax({ url: form.action, type: form.method, data: $(form).serialize(), success: function(response) { $(‘#answers’).html(response); } }); } }); The jQuery.validate plugin will invoke the submit handler … Read more

jQuery form validation on button click

Within your click handler, the mistake is the .validate() method; it only initializes the plugin, it does not validate the form. To eliminate the need to have a submit button within the form, use .valid() to trigger a validation check… $(‘#btn’).on(‘click’, function() { $(“#form1”).valid(); }); jsFiddle Demo .validate() – to initialize the plugin (with options) … Read more

JQuery Validate multiple fields with one error

Similar to Chris’s $(“form”).validate({ rules: { DayOfBirth: { required: true }, MonthOfBirth: { required: true }, YearOfBirth: { required: true } }, groups: { DateofBirth: “DayOfBirth MonthOfBirth YearOfBirth” }, errorPlacement: function(error, element) { if (element.attr(“name”) == “DayOfBirth” || element.attr(“name”) == “MonthOfBirth” || element.attr(“name”) == “YearOfBirth”) error.insertAfter(“#YearOfBirth”); else error.insertAfter(element); } });

jQuery Validation: Changing Rules Dynamically

Ahh validation plugin, always so tricky 🙁 First, I added id attributes to all the input boxes. Then, I made a change function for you: $(“input[name=”userAction”]”).change(function() { $(‘#signupFields’).toggle(); $(‘#loginFields’).toggle(); if ($(“input[name=”userAction”]:checked”).val() === “login”) { removeRules(signupRules); addRules(loginRules); } else { removeRules(loginRules); addRules(signupRules); } }); The add and remove functions look like this: function addRules(rulesObj){ for (var … Read more

Unobtrusive validation in Chrome won’t validate with dd/mm/yyyy

Four hours later I finally stumbled across the answer. For some reason Chrome seems to have some inbuilt predilection to use US date formats where IE and FireFox are able to be sensible and use the regional settings on the OS. jQuery.validator.methods[“date”] = function (value, element) { return true; }

jQuery validator and a custom rule that uses AJAX

For anyone else who stumbles upon this, validate supports ‘remote’ method, which may not have existed in 2010: https://jqueryvalidation.org/remote-method/ $(“#myform”).validate({ rules: { email: { required: true, email: true, remote: { url: “check-email.php”, type: “post”, data: { username: function() { return $(“#username”).val(); } } } } } });