From: Geoff Kimball Date: Wed, 2 Mar 2016 01:21:10 +0000 (-0800) Subject: Fix Abide custom patterns not validating properly #8157 X-Git-Tag: v6.2.1~43^2~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4de9ac68cf1e2552918816461e1062f3277a501c;p=thirdparty%2Ffoundation%2Ffoundation-sites.git Fix Abide custom patterns not validating properly #8157 --- diff --git a/js/foundation.abide.js b/js/foundation.abide.js index 685045bac..4201d0b86 100644 --- a/js/foundation.abide.js +++ b/js/foundation.abide.js @@ -268,17 +268,30 @@ class Abide { * @returns {Boolean} Boolean value depends on whether or not the input value matches the pattern specified */ validateText($el, pattern) { - // pattern = pattern ? pattern : $el.attr('pattern') ? $el.attr('pattern') : $el.attr('type'); + // A pattern can be passed to this function, or it will be infered from the input's "pattern" attribute, or it's "type" attribute pattern = (pattern || $el.attr('pattern') || $el.attr('type')); var inputText = $el.val(); + var valid = false; + + if (inputText.length) { + // If the pattern attribute on the element is in Abide's list of patterns, then test that regexp + if (this.options.patterns.hasOwnProperty(pattern)) { + valid = this.options.patterns[pattern].test(inputText); + } + // If the pattern name isn't also the type attribute of the field, then test it as a regexp + else if (pattern !== $el.attr('type')) { + valid = new RegExp(pattern).test(inputText); + } + else { + valid = true; + } + } + // An empty field is valid if it's not required + else if (!$el.prop('required')) { + valid = true; + } - // if text, check if the pattern exists, if so, test it, if no text or no pattern, return true. - return inputText.length ? - this.options.patterns.hasOwnProperty(pattern) ? this.options.patterns[pattern].test(inputText) : - pattern && pattern !== $el.attr('type') ? - new RegExp(pattern).test(inputText) : - true : - true; + return valid; } /** diff --git a/test/visual/abide/text.html b/test/visual/abide/text.html new file mode 100644 index 000000000..f52d9eb14 --- /dev/null +++ b/test/visual/abide/text.html @@ -0,0 +1,41 @@ + + + + + + + Foundation for Sites Testing + + + +
+

Abide: Text Fields

+ +

This form has one required text field and one optional text field.

+ +
+ + + + +
+ +
+ +

This field uses a custom pattern. The field is valid if the input is between 4 and 16 characters long.

+ +
+ + + +
+
+ + + + + +