5

I am using jquery and validate to validate my form. Instead of adding an element to the missing field I am adding a border to the element using this option errorPlacement

$("#signup-form").validate({
       submitHandler: function(form) {
      form.submit();
   },
   errorPlacement: function(error, element) {
      element.css("border", "solid 1px red");
   }                    
});

the one thing i can not seem to figure out is how to clear it when valid?

So I started with the above code. Then tried and i am confused with the results. If I do not have the success: option when a field fails it adds the class successfully. But as soon as I add the success option all the required fields are getting that class, and if I inspect the element I see <input class="required invalid valid"> so I am doing something incorrectly.

           $("#signup-form").validate({
                submitHandler: function(form) {
                    // do other stuff for a valid form
                    //form.submit();
                },
                errorPlacement: function(error, element) {
                    element.addClass("invalid");
                },        
                success: function(error) {
                    // change CSS on your element(s) back to normal
                },                    
                 debug:true
            });
4
  • Normally, by default, the error clears itself. Please explain more and/or show a demo of your problem. Commented Aug 28, 2012 at 15:43
  • i am adding the code above. so when a field fails instead if adding aa new element i am just changing the border of the missing field to red. if i put in the info and leave the field it still has the border. Commented Aug 28, 2012 at 15:46
  • 1
    You can refer to the answer here: stackoverflow.com/questions/3518895/… The success option has a callback which you can use to disable the label's border. Commented Aug 28, 2012 at 16:36
  • It solves my problems. But I cannot say why :) Commented Sep 20, 2016 at 15:55

2 Answers 2

7

You could try adding this to your list of .validate() options...

success: function(error) {
    // change CSS on your element(s) back to normal
}

This should work dynamically without a form submit. As soon as the error is resolved, the contained code will run.

See documentation.

Please create a demo of your problem using jsFiddle. Here's a blank jsFiddle with .validate() already included and ready for use.


EDIT

As per OP's jsFiddle, I made these changes...

    errorPlacement: function(error, element) {
        $(element).filter(':not(.valid)').addClass("invalid");
    },
    success: function(error) {
        console.log(error);
        $("#signup-form").find('.valid').removeClass("invalid");
    }

Working Demo: http://jsfiddle.net/u3Td3/6/

Side-notes: You also had some HTML issues. You should "self-close" your input elements with />. Same thing for the submit button, self-close rather than using a </input>. See validator.w3.org to validate your HTML. I'd also not use a table for layouts, use CSS.

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

4 Comments

@randy, since I can't see your HTML or your page, I can't write an exact solution. Use jsFiddle to create a demo of your problem. Here's a blank jsFiddle with .validate() already included and ready for use.
thanks you example works except when the text fields are pre-filled. getting closer jsfiddle.net/paries/u3Td3/8
I don't think that's anything to do with success, custom error placement, or your original question. The new question is 'why is the invalid class being applied to non-empty fields on submit?'
I'm glad I could help with this part at least.
0
<form id="signupForm">
    <div class="form-group">
        <label for="contact">Your Name <em>(required)</em></label>
        <input type="text" class="form-control" id="contact" name="contact" placeholder="Name" required>
    </div>
</form>

$('#serviceForm').validate({
    errorPlacement: function (error, element) {
        element.parent().addClass("hasError");
    },
    success: function (error, element) {
        $(element).parent().removeClass("hasError");
    }
})

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.