Reputation: 5329
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:
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.
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?
Upvotes: 0
Views: 1088
Reputation: 98738
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.
Upvotes: 1