0

We are using jQuery Validate Plugin for all client side validations.

Until now, we were using default error message for all the required fields (i.e. 'This field is required'), but now we need to change it to field specific error message. For example, if user doesn't enter first name, the message should be displayed as Please enter first name and so on.

These are the possibilities we have tried:

  1. Using HTML 5 attributes data-rule-required="true" and data-msg-required="Madam/sir, this field is required.". Due to some other restrictions, we have to avoid using HTML 5 attributes.

  2. Providing messages object while validating form: messages: { username: {required: "Please enter Username",email: "Enter valid Email"}, password: "Please enter Password" }, The disadvantage of this method is that we need to modify both html and the javascipt files and the field names must be in sync for proper functining of the plugin.

I am looking for a similar solution to that of HTML 5 attributes - where I can specify a custom attribute to my input fields, the plugin will pick up the message from that field itself. Something like this:

<input type="text" required errorMessageCustomAttr="Please enter first name" />

I have referred to relevant SO questions, but unable to find any solution. Is there any way to achieve this?

3
  • If you do not want to use HTML5 required nor managing a .js file among with corresponding HTML as a solution - then you have dug yourself into a pit difficult to get out of.. these are probably the primary solutions out there.. Commented Jan 15, 2015 at 12:40
  • I do not understand why you cannot use HTML5 attributes but you're fine with using a custom attribute. An inline attribute is an inline attribute. Since the jQuery Validate plugin dynamically disables HTML5 validation, the HTML5 attributes are simply used like custom attributes anyway. Commented Jan 15, 2015 at 17:44
  • Why can't you use HTML5? Your reason may affect a possible solution. Commented Jan 15, 2015 at 21:14

1 Answer 1

1

Quote OP:

"I am looking for a similar solution to that of HTML 5 attributes - where I can specify a custom attribute to my input fields, the plugin will pick up the message from that field itself."

The answer is "no", this is not an option of the plugin.

You would use the HTML5 attribute for the custom messages. Since HTML5 validation is dynamically disabled by the jQuery Validate plugin, the HTML5 data attribute simply becomes a custom attribute.

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

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.