0

I am trying to keep the default error message placement and behavior when using jQuery Validate plugin. When I set any properties of the validate() object the default behavior seems to change.

For example. If I simply decorate my html input with the required attribute here are the results:

HTML attribute:

<input id="name" type="text" required/>

Dedfault jQuery Validate Behavior

Once I modify a property of the validate object here is what I get:

After Validate Property Change

The code I am using to modify:

$("[id$='emailForm']").validate({
    debug: true,
    rules: {
        txtEmail: {
            required: true,
            email: true
        }
    }
});

So what gives. Why does all the formatting and placement change. What is the most simple way I can get it back to how it behaves by default?

5
  • You don't show nearly enough code to explain your pictures. The jQuery code alone can not do what you've shown, and this is the true default behavior => jsfiddle.net/6UAs3 Commented Nov 21, 2013 at 18:45
  • I can add the exact HTML attribute, but other than what I put in, there is no additional code. This is how jQuery validate works out of the box. Commented Nov 21, 2013 at 18:49
  • What you think is the default behavior of the jQuery Validate plugin is no such thing. Your first picture is the default HTML5 validation supplied by your HTML5 compliant browser. Your second picture is the default behavior of the jQuery Validation plugin. Commented Nov 21, 2013 at 18:51
  • Oh man, this explains it. Shows I do not know HTML 5 well enough then. Thank you. Commented Nov 21, 2013 at 18:53
  • 1
    See this question/answer for how to achieve the look you want while using the jQuery Validation plugin: stackoverflow.com/q/14741688/594235 Commented Nov 21, 2013 at 18:57

2 Answers 2

1

Your code:

<input id="name" type="text" required/>

required is an HTML5 validation attribute. And what you see in your first picture is only what your HTML5 compliant browser is doing... it has nothing to do with the plugin. The plugin does nothing until you call the .validate() method to initialize it.

Once you call .validate() method, you are initializing jQuery Validation plugin and then it takes over. Your second picture is the default behavior of the plugin.

Demo of default look of the plugin: http://jsfiddle.net/6UAs3/


See this question/answer for how to achieve the "look" you want while using the jQuery Validation plugin: How to display messages from jQuery Validate plugin inside of Tooltipster tooltips?

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

Comments

-2

When you create the validate object for the first time, save it in a variable and use it later for triggering validations.

var validator = $("[id$='emailForm']").validate({
    debug: true,
    rules: {
        txtEmail: {
            required: true,
            email: true
        }
    }
});

// Trigger validation at later point by invoking the valid method instead of validate
var isValid = validator.valid();

2 Comments

So this will keep the default Validate behavior?
Yes. But that depends on what you mean by default. If you solely rely on html 'required' attribute, you do not need the validator plugin. I am not sure what browsers your application has to support, but you cannot rely on the required attribute to work correctly in old browsers. Each call to validate method will apply default validations afresh on the element(s). The key is to call validate once and then use the validator object that it returns and call "valid()" method - that will return a boolean value.

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.