How to force single error using jQuery validation?
I am using the jQuery validation plugin on my form. I set the basic rules and messages via javascript like this:
rules:{
"billing.firstname":{
required:true
开发者_如何转开发},
messages:{
"billing.firstname":"First name is required."}
I have the additional requirement to use a javascript function to show a field as invalid with a specified message which I am accomplishing using validator.addMethod then calling validate().element(element) on the field in question.
The problem I am having is that I am unable to override the message set initially with the one provided in the addMethod call. So while my added rule is being used to validate, the original 'First name is required' message always displays even if I clear all rules prior to adding the new one.
Is there a way to force validation of a single rule on a single element or alternatively somehow access the message assigned so I can change it temporarily?
For reference the overall goal is there are some other ajax type functions that happen on the page out of my control and I need to allow them to use a JS function to manually set a field invalid state and message.
Thanks in advance -
b
FYI - I figured out how to do this. It is via a method that is not documented for the plugin, but was the only way I could find to access the initially set messages.
I'm currently using
form.validate().settings.messages[elementId]
to get the message for a specific element as initially set and setting it to something different while maintaining the original as a .data part of the element itself. This way onChange I can remove the custom message and reset to the original.
b
精彩评论