add if (this.optional(element)) return true; to the top. See any existing validation method for an example, or http://docs.jquery.com/Plugins/Validation/Validator/addMethod
Jörn On Wed, Mar 11, 2009 at 12:43 PM, clorentzen <carl.lorent...@gmail.com> wrote: > > Hi -- > > I've got a contact form here > > http://www.dianlofton.com/contact.shtml > > ...using the jquery.validate.js plugin. The form has an optional input > for a phone number, which I'd like to have validated -- but only if > there is info in the input. If you look at the source code for that > page, you'll see some commented out code for the phone number > validation. However, when this scripting is active on the page, it > makes the phone number a required field, which is not what I'm after: > > $.validator.addMethod("phone", function(ph, element) { > if (ph == null) { > return false; > } > var stripped = ph.replace(/[\s()+-]|ext\.?/gi, ""); > // 10 is the minimum number of numbers required > return ((/\d{10,}/i).test(stripped)); > }, "Please enter a valid phone number"); > > Any help on how to have this field be optional, but still get > validated if the user inputs data, would be greatly appreciated. > Thanks! > > --Carl. > >