Skip to content

Commit 4a2b9d4

Browse files
committed
Added Google reCaptcha 2 validator
1 parent da04010 commit 4a2b9d4

File tree

5 files changed

+73
-14
lines changed

5 files changed

+73
-14
lines changed

form-validator/jquery.form-validator.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@
265265
inputIsOptional = $elem.valAttr('optional') === 'true',
266266
skipBecauseDependingInputIsEmpty = false,
267267
skipBecauseItsEmpty = !value && inputIsOptional,
268-
skipBecauseInputIsHidden = $elem.attr('disabled') || (!$elem.is(':visible') && !conf.validateHiddenInputs),
268+
validationRules = $elem.attr(conf.validationRuleAttribute),
269+
skipBecauseInputIsHidden = $elem.attr('disabled') || (!$elem.is(':visible') && !conf.validateHiddenInputs && (typeof validationRules !== typeof undefined && validationRules.indexOf("recaptcha") < 0)),
269270
validationDependsOn = $elem.valAttr('depends-on') || $elem.valAttr('if-checked');
270271

271272
if (skipBecauseInputIsHidden) {
@@ -303,10 +304,8 @@
303304
return result;
304305
}
305306

306-
var validationRules = $elem.attr(conf.validationRuleAttribute),
307-
308307
// see if form element has inline err msg attribute
309-
validationErrorMsg = true;
308+
var validationErrorMsg = true;
310309

311310
if (!validationRules) {
312311
result.shouldChangeDisplay = conf.addValidClassOnAll;

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: 65 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,74 @@
512509
errorMessageKey: 'requiredFields'
513510
});
514511

512+
/*
513+
* Google reCaptcha 2
514+
*/
515+
$.formUtils.addValidator({
516+
name: 'recaptcha',
517+
validatorFunction: function (val, $el, config)
518+
{
519+
return grecaptcha.getResponse($el.data('validation-widget-id'));
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+
var src = '//www.google.com/recaptcha/api.js?onload=reCaptchaLoaded&render=explicit' + (config.lang ? '&hl=' + config.lang : '');
533+
if ($('body').find('script[src="' + src + '"]').length === 0)
534+
{
535+
var script = document.createElement('script');
536+
script.type = 'text/javascript';
537+
script.async = true;
538+
script.defer = true;
539+
script.src = src;
540+
document.getElementsByTagName('body')[0].appendChild(script);
541+
}
542+
};
543+
544+
window.reCaptchaLoaded = function ()
545+
{
546+
var $forms = $('form');
547+
548+
if (!$forms.each)
549+
{
550+
$forms = $($forms);
551+
}
552+
553+
$forms.each(function ()
554+
{
555+
var $form = $(this),
556+
config = $form.context.validationConfig,
557+
sitekey = config.reCaptchaSiteKey,
558+
theme = config.reCaptchaTheme;
559+
560+
$('[data-validation~="recaptcha"]', $form).each(function ()
561+
{
562+
var $el = $(this),
563+
div = document.createElement("DIV");
564+
565+
$el.hide();
566+
$el.parent().append(div);
567+
568+
var widget_id = grecaptcha.render(div, {
569+
sitekey: sitekey || $el.valAttr('sitekey'),
570+
theme: theme || $el.valAttr('recaptcha-theme') || 'light'
571+
});
572+
573+
$el
574+
.data('validation-widget-id', widget_id);
575+
});
576+
577+
});
578+
};
579+
580+
$(window).on('validatorsLoaded formValidationSetup', setupGooglereCaptcha);
581+
522582
})(jQuery, window);

0 commit comments

Comments
 (0)