Validate however you want, but NEVER rely on JavaScript to do all of your validation because you cannot rely on it always being available[1]. Always validate on the server (no malicious hacker can bypass that), and use JavaScript to enhance the user experience of filling out your forms (and decrease server load).
If there's anything besides digits, dollar signs, commas, and periods in the
input, I want an error to be thrown.
This should give you what you're after: function superCoolValidator(value, element, params) { if(/[^0-9\$\.,]/g.test(value)) { // value contains something that is neither a digit, ($), (.), or (,) return false; } else { return true; } } If you want to restrict input to only digits, then Jorn's Validator already has a rule that you can use: "digits" P.S. I hate to be petty, but superCoolValidator was my name, not Dan's [1] http://www.456bereastreet.com/archive/200612/you_cannot_rely_on_javascript_being_available_period/ On 4/11/07, Rick Faircloth <[EMAIL PROTECTED]> wrote:
Actually, I didn't want what's left after stripping all of the ($), (.), and (,). I wanted an error to be thrown. However, that approach was taken when I was attempting to use ColdFusion to do the parsing.� I wasn't able to get that working for now, so I decided to go with client-side validation only using the functions of Javascript and Jorn's Validation plug-in. I decided it would be preferable to require digits only since ColdFusion and Javascript won't validate a dollar amount in the same manner. Rick *From:* jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] *On Behalf Of *Aaron Heimlich *Sent:* Wednesday, April 11, 2007 10:01 AM *To:* jquery-en@googlegroups.com *Subject:* [jQuery] Re: Trying to figure out how to use this custom validation... You should have it the way it was. parseInt tries to convert what's left after stripping all of the ($), (.), and (,), into an integer. If it fails, it will return NaN (Not a Number). On 4/10/07, *Rick Faircloth* <[EMAIL PROTECTED]> wrote: Well, it looks like I'm making progress... I needed to change if ( isNaN ( parseInt ( value.replace ( /[\$\,\.]/, "" )))) to if ( isNaN ( value.replace ( /[\$\,\.]/, "" ))) because I don't want to parse what's left. If there's anything besides digits, dollar signs, commas, and periods in the input, I want an error to be thrown. However, the problem I have now is the variations on the input that should pass validation... Here are some examples: $255900 passes as a valid entry. $255,900 does not pass and I want it to. $255,900. does not pass and I want it to. $255,900.00 does not pass and I want it to. But why? The statement if ( isNaN ( value.replace (/[\$\,\.]/, "" ))) is stating that "if all dollar signs, commas, and periods are taken out of the input entry, and the input is not a number"... right? Rick -----Original Message----- From: jquery-en@googlegroups.com [mailto:jquery-en@googlegroups.com ] On Behalf Of J�rn Zaefferer Sent: Tuesday, April 10, 2007 5:41 PM To: jquery-en@googlegroups.com Subject: [jQuery] Re: Trying to figure out how to use this custom validation... Dan G. Switzer, II schrieb: > Rick, > > >> Here's the addMethod line: >> >> $.validator.addMethod("superCoolValidator", superCoolValidator, >> "Your input is not super cool!"); >> >> rules: { >> >> //Principal: {required: true, >> //digits: true}, >> Interest: {required: true, >> number: true}, >> Years: {required: true, >> number: true} >> }, >> > > I haven't messed with the custom validators in J�rn's code, but looking > through the code, you'll need to assign a rule for the validator: > > rules: { > > Principal: {required: true, > superCoolValidator: true, > digits: true}, > Interest: {required: true, > number: true}, > Years: {required: true, > number: true} > }, > Good job Dan, didn't notice that. Rick: When you specify rules via plugin options, metadata is ignored. I thought I documented that somewhere... Gonna fix that. -- J�rn Zaefferer http://bassistance.de -- Aaron Heimlich Web Developer [EMAIL PROTECTED] http://aheimlich.freepgs.com
-- Aaron Heimlich Web Developer [EMAIL PROTECTED] http://aheimlich.freepgs.com