jQuery validate custom method - either/or but Not both
I'm banging my head against a custom validation method. It's not giving me any errors, and the rest of the validation works. I need to pass the validation if one or the other form field is filled out, but NOT both. I've found several examples of either/or OR both here, and elsewhere on the net, but nothing directly applicable to this situation. I've also read several other addMethod tutorials, the biggest hole I've found is that none of the 20-30 examples I've read show an interaction with other validate rules, which I believe may be part of my problem.
Below is the relevant scrap of code; and I have a few (potentially n00b) questions:
- Can a group co-exist like this with other "normal" validation rules? If so, does the group need to be mentioned in the ruleset?
- In the addClassRules section, why is the rule name (requiredNotBoth in this case) never referenced anywhere else? I chose that form based on the multiple fields example in the jQuery docs, is it expected to be required-methodName-?
Finally, am I doing the comparison right in the first place? Is there a better/cleaner/prettier way to make that determination?
$.validator.addMethod("NotBoth", function() 开发者_Go百科{ return ((($('#amtfield').val() != "") && ($('#pctfield').val() == "")) || (($('#amtfield').val() == "") && ($('#pctfield').val() != ""))); }, " Please enter either a dollar amount or percentage for the discount."); $.validator.addClassRules({ requiredNotBoth: {digits:true, NotBoth:true} }); $(document).ready(function(){ $("#EditForm").validate({ groups: { NotBoth: "pctfield amtfield" }, rules:{ codetextfield: {required: true}, uselimitfield: {required: true}, }, --snip--
Many many thanks for any help on this, it's driving me nuts
精彩评论