0

The validate plugin on error also adds errorClass to input field. How to avoid this?

var validator = $("#contacts_form").validate({
        submitHandler: function(form) {
            form.submit();  //Must be called here,
        },
        errorElement: "p",
        errorClass : 'form-item-validation',
        rules: {
            name: "required",
            website: {
                required: true,
                minlength: 5
            },
            email: {
                required: true,
                email: true
            },
            onchange: true
        },
        messages: {
            name: "Please enter your firstname",
            email: {
                email: "Please enter a valid email address"
            }
        }
    });
5
  • You could set it to an empty string, however it's worth noting that the same class is applied to the form controls as it is to the label elements. Therefore if you need them to be different, that's not possible. I'm not sure why you'd need to avoid the class on the form elements anyway. Just amend the CSS rules if it's a UI issues Commented Mar 17, 2020 at 10:21
  • @RoryMcCrossan well, errorClass : 'form-item-validation' has styles and thats breaks all, I only need add error class to the message container but not to the input Commented Mar 17, 2020 at 10:23
  • 'form-item-validation' has styles and thats breaks all in that case just change the CSS as you need. Commented Mar 17, 2020 at 10:24
  • @RoryMcCrossan its not a case to change css, I didn't create them and I don't know who will add or replace them Commented Mar 17, 2020 at 10:38
  • I presume onchange is not one of your field names? Please note that only field names followed by their methods go inside of the rules object, so basically, onchange: true makes absolutely no sense inside there. Secondly, onchange is not even an option for this plugin: jqueryvalidation.org/?s=onchange Commented Mar 17, 2020 at 17:41

1 Answer 1

0

My solution is add to the settings

        showErrors: function(errorMap, errorList) {
            this.defaultShowErrors();
            $('input').removeClass('form-item-validation');
        },
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, highlight and unhighlight are the two options you should use for adding and removing classes during validation.
showErrors does it too actually or there are some hidden stones?
Look at the source code. The two I mentioned already are the methods for reliably adding/removing classes at the proper validation events.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.