Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show errors in jQuery validation?

Do we have any function which returns all the error messages while validating a form?

I have tried using the defaultshowerros() function but it returns the error message for the element it is currently validating. How can I get all the error messages of the whole form?

like image 442
Vaibhav Jain Avatar asked Sep 28 '10 08:09

Vaibhav Jain


People also ask

How can I show error message below the textbox in jQuery validation?

Java scriptready(function() { $("#basic-form"). validate(); }); This is based on the assumption that you have already added the required JavaScript files. Adding those lines of JavaScript will make sure that your form is properly validated and shows all the error messages.

How do you check if jQuery validate is working?

fn , so simply check whether it exists or not; if (typeof jQuery. fn. validationPlugin === "function") { // It's loaded } else { // It's not. }

What jQuery Unobstructive validation is?

An unobtrusive validation in jQuery is a set of ASP.Net MVC HTML helper extensions.By using jQuery Validation data attributes along with HTML 5 data attributes, you can perform validation to the client-side.


3 Answers

If you store a reference to the validator, for example:

var validator = $("form").validate();

You can call .errors() or .invalidElements() at any time on it, for example:

var errors = validator.errors(); //get the error elements, the actual labels
var errors = validator.invalidElements(); //the invalid elements themselves

If you're not really after the errors and just want them to appear in once place, use the built-in errorLabelContainer and wrapper options, for example:

<ul id="errors"></ul>

And reference that:

$("form").validate({ errorLabelContainer: "#errors", wrapper: "li" });

And your errors would appear all in that list, which is also automatically shown/hidden if there are/aren't any errors.

like image 140
Nick Craver Avatar answered Sep 20 '22 10:09

Nick Craver


The validation plugin should show an error beside the field where the error is. Are you using id's for your input boxes? If so use a name as well and give jquery the value of the name attribute in your rules and messages. Hope this helps.

like image 43
Belinda Avatar answered Sep 19 '22 10:09

Belinda


Late to the party, but I found you can also instantiate the validate() object with a invalidHandler() function:

var $jqvForm = $(".jqvForm").validate({
    invalidHandler: function(e, validation){
        console.log("invalidHandler : event", e);
        console.log("invalidHandler : validation", validation);
    }
});

The validation variable contains a variable invalid (object) with form items and their error messages.

like image 41
Sean Kendle Avatar answered Sep 18 '22 10:09

Sean Kendle