Skip to content

Commit a87b9ce

Browse files
committed
Merge pull request victorjonsson#399 from simivar/master
Add Google reCaptcha 2 validator
2 parents da04010 + 22483be commit a87b9ce

File tree

5 files changed

+79
-10
lines changed

5 files changed

+79
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Read the documentation for the default features at [http://formvalidator.net/#de
7676
* **strength***Validate the strength of a password*
7777
* **server***Validate value of input on server side*
7878
* **letternumeric***Validate that the input value consists out of only letters and/or numbers*
79+
* **recaptcha** - *Validate Google [reCaptcha 2](https://www.google.com/recaptcha/intro/index.html)*
7980

8081
Read the documentation for the security module at [http://formvalidator.net/#security-validators](http://formvalidator.net/#security-validators)
8182

form-validator/jquery.form-validator.min.js

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lang/pl.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
badBrazilCPFAnswer: 'Wprowadzono niepoprawny CPF',
6262
badPlPesel: 'Wprowadzono niepoprawny numer PESEL',
6363
badPlNip: 'Wprowadzono niepoprawny numer NIP',
64-
badPlRegon: 'Wprowadzono niepoprawny numer REGON'
64+
badPlRegon: 'Wprowadzono niepoprawny numer REGON',
65+
badreCaptcha: 'Potwierdź że nie jesteś botem!'
6566
};
6667

6768
});

src/main/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,8 @@
830830
badBrazilCPFAnswer: 'The CPF entered is invalid',
831831
badPlPesel: 'The PESEL entered is invalid',
832832
badPlNip: 'The NIP entered is invalid',
833-
badPlRegon: 'The REGON entered is invalid'
833+
badPlRegon: 'The REGON entered is invalid',
834+
badreCaptcha: 'Please confirm that you are not a bot'
834835
}
835836
});
836837

src/modules/security.js

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
errorMessageKey: 'badSecurityAnswer'
3333
});
3434

35-
3635
/*
3736
* Validate confirmation
3837
*/
@@ -137,7 +136,6 @@
137136
errorMessageKey: 'badCreditCard'
138137
});
139138

140-
141139
/*
142140
* Credit card number
143141
*/
@@ -479,7 +477,6 @@
479477
validateOnKeyUp : false
480478
});
481479

482-
483480
/*
484481
* Check for only letters and numbers
485482
*
@@ -512,11 +509,82 @@
512509
errorMessageKey: 'requiredFields'
513510
});
514511

512+
/*
513+
* Google reCaptcha 2
514+
*/
515+
$.formUtils.addValidator({
516+
name: 'recaptcha',
517+
validatorFunction: function (val, $el)
518+
{
519+
return grecaptcha.getResponse($el.valAttr('recaptcha-widgetId'));
520+
},
521+
errorMessage: '',
522+
errorMessageKey: 'badreCaptcha'
523+
});
515524

516-
517-
$.fn.displayPasswordStrength = function(conf) {
525+
$.fn.displayPasswordStrength = function(conf) {
518526
new $.formUtils.validators.validate_strength.strengthDisplay(this, conf);
519527
return this;
520528
};
521529

530+
var setupGooglereCaptcha = function (evt, $forms, config)
531+
{
532+
if( typeof grecaptcha !== typeof undefined && !$.formUtils.hasLoadedGrecaptcha ){
533+
throw new Error('reCaptcha API can not be loaded by hand, delete reCaptcha API snippet.');
534+
} else if(!$.formUtils.hasLoadedGrecaptcha)
535+
{
536+
$.formUtils.hasLoadedGrecaptcha = true;
537+
538+
var src = '//www.google.com/recaptcha/api.js?onload=reCaptchaLoaded&render=explicit' + (config.lang ? '&hl=' + config.lang : '');
539+
var script = document.createElement('script');
540+
script.type = 'text/javascript';
541+
script.async = true;
542+
script.defer = true;
543+
script.src = src;
544+
document.getElementsByTagName('body')[0].appendChild(script);
545+
}
546+
};
547+
548+
window.reCaptchaLoaded = function ()
549+
{
550+
var $forms = $('form');
551+
552+
if (!$forms.each)
553+
{
554+
$forms = $($forms);
555+
}
556+
557+
$forms.each(function ()
558+
{
559+
var $form = $(this),
560+
config = $form.context.validationConfig;
561+
562+
$('[data-validation~="recaptcha"]', $form).each(function ()
563+
{
564+
var $el = $(this),
565+
div = document.createElement('DIV'),
566+
siteKey = config.reCaptchaSiteKey || $el.valAttr('recaptcha-sitekey'),
567+
theme = config.reCaptchaTheme || $el.valAttr('recaptcha-theme') || 'light';
568+
569+
if( !siteKey ){
570+
throw new Error('Google reCaptcha site key is required.');
571+
}
572+
573+
$el.hide();
574+
$el.parent().append(div);
575+
576+
var widgetId = grecaptcha.render(div, {
577+
sitekey: siteKey,
578+
theme: theme
579+
});
580+
581+
$el
582+
.valAttr('recaptcha-widgetId', widgetId);
583+
});
584+
585+
});
586+
};
587+
588+
$(window).on('validatorsLoaded formValidationSetup', setupGooglereCaptcha);
589+
522590
})(jQuery, window);

0 commit comments

Comments
 (0)