views:

441

answers:

2

I added a captcha to my form and I want to validate this field.

jQuery.validator.addMethod("matchCaptcha", function(value,element)  {
    return  this.optional(element) || value == '#request.strCaptcha#';
});

#request.strCaptcha# this part simply returns an alphanumeric char something like 'cZk9ljW'

under the rules, i added:

captcha : { matchCaptcha : true }

The validation does not run and I get no errors from script. What am I doing wrong? thanks

+1  A: 

Everything you posted sounds fine, which probably means it's something you haven't posted.

  1. You're doing your addMethod inside a $(document).ready(), right?
  2. Have you run this with a debug alert() inside the validation function to verify whether it is actually not running, as opposed to running and always returning true?
  3. The input name for the captcha's text area is captcha, yeah?

If you do add an alert() and find the validation method is being called, my recommendation for you is to just get rid of the this.optional(element) || part. I don't imagine you ever want your captcha to be optional anyway.

chaos
A: 

This one was tricky a little bit. I am using ColdFusion. I created a custom tag to create and display the captcha. I removed the jQuery.validator.addMethod to after my custom tag call. then I got rid of this.optional(element) || part. Also, had to add another $().ready(function() { ... });

So, it looks like:

$().ready(function() { jQuery.validator.addMethod("matchCaptcha", function(value,element) { return value == '#request.strCaptcha#'; }); });

I still have captcha : { matchCaptcha : true } over in the validation rules. Thanks for the heads up.

FALCONSEYE