2

I have the jQuery Validation plugin:

//name validation
cnForm.validate({
    rules: {
        customerName: {
            required: true,
            minlength: 3
        },
        date: {
            required: true,
            dateRegex: /^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d$/,
            maxlength: 10
        }               
    },
    onkeyup: false,
    onclick: false,
    onfocusout: false,
    messages: {
        customerName: {
            required: "Unable to proceed, search field is empty"
        },
        date: {
            required: "Unable to proceed, search field is empty"
        }   
    },
    errorPlacement: function(error, element) {
        var trigger = element.next('.ui-datepicker-trigger');
        error.insertAfter(trigger.length > 0 ? trigger : element);
    }
});

Whenever I input data that causes an error, I get the appropriate new label to display after the input box/button, but the class error is also applied to the input box. Is there a way to avoid assigning that class to the box? Thanks.

3 Answers 3

6

You can use highlight call back to override the default behavior.

$(".selector").validate({
    highlight: function(element, errorClass) {
        // Override the default behavior here
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

nice, I just added that and put a //do nothing comment in the method.
@JoeEssey, that's not a very good way to do it. At least put a return false in there.
Thanks for the recommendation @aarryy. If you have a moment can you please explain why? Is it so there is never some type error thrown when a function is called?
@JoeEssey, it's a matter of proper programming practices/habits... why would you want an empty function? Maybe this time it works... maybe next time it chokes a future revision of the plugin.
Hi JoeEssey, I supposed you were asking @Sparky. I actually use the same way if I just want to suppress the default behavior so I'm curious as well
|
6

"...but the class error is also applied to the input box. Is there a way to avoid assigning that class to the box?"

Use the highlight and unhighlight callback functions to override the default functions.

These are the defaults...

    highlight: function (element, errorClass, validClass) {
        if (element.attr('type') === "radio") {
            this.findByName(element.attr('name')).addClass(errorClass).removeClass(validClass);
        } else {
            $(element).addClass(errorClass).removeClass(validClass);
        }
    },
    unhighlight: function (element, errorClass, validClass) {
        if (element.attr('type') === "radio") {
            this.findByName(element.attr('name')).removeClass(errorClass).addClass(validClass);
        } else {
            $(element).removeClass(errorClass).addClass(validClass);
        }
    }

As per docs...

The callback gets passed three arguments:

  • element
    Type: Element
    The invalid DOM element, usually an input.

  • errorClass
    Type: String
    Current value of the errorClass option.

  • validClass
    Type: String
    Current value of the validClass option.


EDIT:

To entirely remove this functionality...

$('#myform').validate({
    highlight: function (element, errorClass, validClass) {
        return false;  // ensure this function stops
    },
    unhighlight: function (element, errorClass, validClass) {
        return false;  // ensure this function stops
    },
    // other options
});

Comments

1

"...but the class error is also applied to the input box. Is there a way to avoid assigning that class to the box?"

$('#formId').validate({
        errorElement: 'span',
        errorClass: 'errorMessage',
        errorPlacement: function (error, element) {
            error.insertAfter(element);
        },
        highlight: function (element, errorClass, validClass) {
            $(element).removeClass(errorClass);
        },
        ...
});

Comments

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.