killebytes
killebytes

Reputation: 980

jQuery validation get id/name of element

I want to get the id/name of the element in error. I need it to display it as a list so the default message "this is required" is not informative.

I am using this to display the message:

$.validator.message.required = [custom message]

I have tried using

$.validator.message.required = $.validator.format("error is {0}{1}")

but this only returns a string

"error is [object HTMLInputElement]"

Upvotes: 1

Views: 4946

Answers (4)

Amritesh
Amritesh

Reputation: 97

Just Do this..

window.error_list = $('#userProfile').validate({ ... });
console.log(window.error_list.errorList)

Upvotes: 0

Mike Corcoran
Mike Corcoran

Reputation: 14565

i found this question via googling and actually figured out an answer to this problem.

you can actually just implement your own 'formatting' function and pass that around like you would with what you get back from jQuery.validator.format. your function will get passed two items, the first seems to be a bool (i'm not sure what that is for) and the second param will be the actual html element. so in your case you'd want to do something like this:

$.validator.message.required = function(_, el) {
    console.log(el.name, el.id);
    return 'error is ' + el.name + ' : ' + el.id;
}

Upvotes: 1

killebytes
killebytes

Reputation: 980

I guess this is not possible with jquery.validate

Upvotes: 1

Vinod CG
Vinod CG

Reputation: 1061

use

var id= $("SELECTOR FOR ERROR").attr('id');//for id
var name= $("SELECTOR FOR ERROR").attr('name');//for name

Upvotes: 0

Related Questions