3

I have jquery validation working so that when validation occurs, the errors get placed into a container instead of inline.

I'm having an issue with some of my non-required fields. See the jsfiddle below for the code. Here's a brief overview:

Name: required field
Comment: required field
Url: not required, but class='url' to insure valid url
Email: not required, but class='email' to insure valid email

To reproduce:
Enter the value foo into the email field OR the url field and tab off
You'll get the "email is invalid" error as expected.
Go back into the field and clear it out and tab off
The error will go away, but the error container is still there (you can see the red outline at the top)

It seems that for regex type validation, if the field isn't required, it's not resetting back to ignoring validation when there's no value in there. If I go in and actually fix the error, e.g., making the email [email protected], everything works fine.

How can I get the validation to accept a blank value as valid for non-required fields?

jsfiddle link: http://jsfiddle.net/tjans/napf7/

3
  • I've updated the jsfiddle with some markup more closely relating to what I'm trying to accomplish. I have a bit of a workaround in place that basically just attaches a "change" event to email fields and checks for how many visible divs (visible divs = error) and if the length is 0, hides the errorLabelContainer. It's not perfect, and still has a glitch where it won't actually hide until you tab off the field, so I'd still like to find a better solution. Hopefully the demo update sparks someone's attention. Commented Aug 1, 2012 at 13:49
  • I've tried several events - keyup, unhighlight, etc to do a count of visible divs, but none of them seem to work as they all think that there's one visible div at the time the event runs. Then, when everything is finished, I can do a console query of how many visible divs there are and it shows 0...it's a timing issue at that point. Commented Aug 3, 2012 at 14:33
  • I've also contacted the author of the plugin, but I don't expect to get a response back. I'm sure he's a busy man, or my email got caught by his spam filter or something. Commented Aug 3, 2012 at 14:33

3 Answers 3

2

Use errorClass

jQuery:

$(document).ready(function(){
    $("#commentForm").validate({
        wrapper: 'div',
        errorLabelContainer: '#jserror',
        errorClass: 'errorClass'
    });
  });​

Then move the border css rule to that error class. Style as necessary

#jserror {
    display:none;
}
.errorClass
{
  border:1px solid red;
}

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

This seems to work for my jsfiddle scenario, but when you have multiple errors e.g., url and email, it puts a border around each error. I'll have to see if this can work on the container itself as opposed to each error item.
Furthermore, if you look at the code in either my example or yours in firebug, you'll see that the error never truly goes away...that email error is still sitting there in the markup, so something isn't getting cleared out after fixing the error.
Actually, doing an error count and or looking at firebug, it seems that errors never go away, even for regular "required field" errors, so that might not be a good test.
1

You can overwrite the showErrors method to provide your own functionality. Call the defaultShowErrors for the standard behavior. I wrote a quick version that checks for any errors in the error object hash and conditionally shows or hides the container. Try it out. You may also want to check hasOwnProperty on the object enumeration if you go with this solution.

$("#commentForm").validate({
    wrapper: 'div',
    showErrors: function(errors){
        var hasErrors = false;
        for (var e in errors){
           hasErrors = true;
           break;
        }

        if(hasErrors) $(this.settings.errorLabelContainer).show();
        else $(this.settings.errorLabelContainer).hide();

        this.defaultShowErrors();
    },
    errorLabelContainer: '#jserror'
});

2 Comments

This still seems somewhat odd - if you fill out the email incorrectly, submit the form, fill out the name/comment, then tab up and down the fields a couple times, it hides/shows the errorLabelContainer for some reason...
This is the kind of solutin i was expecting (and tried myself before coming to SO but couldnt get to work). This seems like it should work. I wonder why the random hide/show thing happens.
0

Use unhighlight section in validation plugin.

Check this jsFiddle http://jsfiddle.net/napf7/44/

3 Comments

I was about to get happy, but this will hide validation for other fields as well. Hit submit on empty form. enter foo1 for email and foo2 for url. You should have 3 errors shown...then remove the values for email and url and it'll hide the container, even though name is still invalid. I'm sure I can use unhighlight though to check for errors and then hide...jsfiddle to follow if I can make it happen.
you need to check that too.. jsfiddle.net/napf7/49.. But i don't think its a good method since if your form contains a large number of input fields , then you need to check each case manually..
Thanks, but I don't think that's a suitable, dynamic solution. There has to be a better way...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.