views:

410

answers:

2

Hi,

I'm using jQuery Validation for a form, and I have to display a checkmark next to the input field (if the entry was correct), and the error message (example:"please enter at least 4 characters") below the input field.

I was thinking of doing something like this:

errorPlacement: function(error, element) {
* if the validation is succesful * error.appendTo($("#checkmark")); * otherwise * error.appendTo($("#error-message")); }

But how can you target a different div destination depending on the type of message you need to display? Or do I have it completely wrong and there's another way to do this?

Thanks!

A: 

In the past I've done this by using the validClass option for the valid ones:

$(".selector").validate({
   validClass: "success"
})

success can just be a css class with a checkmark background to the side:

.success {
  background: url(checkmark.png) right no-repeat; /* Say, 16x16px */
  margin-right: 18px;
}
Nick Craver
Thanks, Nick!I think I didn't make myself clear though - the question is if there is a way to display the checkmark in one div and the error message in a different one.Thanks!
Veronica
@Veronica - I'm confused...how can a field pass and fail at the same time? Clarify a bit further?
Nick Craver
Basically, I need to display the checkmark immediately next to the input box and the error message ("please enter at least 4 chars") just below the input box. So I created a <span> and a <div>:<input type="password" id="password"><span class="checkmark"></span><div class="error-message"></div>Maybe I'm doing something fundamentally wrong here?!Thanks for your patience...
Veronica
In my example, you use `validClass:` for the success checkmark with css...the `errorPlacement:` you can still do wherever you want, if you just give the input box a `display: block;` and don't mess with error placement, it'll probably have the effect you describe (unless some other placement you have prohibits it)
Nick Craver
If I don't do anything to the error placement, I will have both the checkmark and the error message in the same place - in my example, right next to the input field. While I want to be able to display the error message below the input field. :(
Veronica
A: 

5 months late...hopefully it's still helpful:

jQuery.validate uses the errorPlacement: option for passing the error and element arguments so you can do whatever you want with them...and the function executes every time an error occurs in the user's form. Its counterpart is success: which passes label as an option.

But for ultimate flexibility you don't even have to use "label". Instead you could pass a jQuery object from errorPlacement (based on traversing from the object "element") to the success: option. And success calculates in real time as well.

All somewhat confusing, but here's some generic code...

For your HTML you could put:

<div class="errorparent">
<input type="text" id="username" name="username"/>
<div class="norederrorx errordetails">&nbsp;</div>
</div>

For locating your custom div under your jQuery.validate options:

errorPlacement: function(error, element) {
   var errorcontent=eval(error);
   errorspotholder = element.parents('.errorparent');

And in your success option:

errorspotholder.find('.rederrorx').removeClass('rederrorx').addClass('norederrorx');

If you want to manipulate even more divs...such as a separate valid mark div, you'd simply traverse to a different object from 'errorsotholder' which refers to the containing parent div 'errorparent'

All the best!

Emile

Emile