]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
Fix Abide custom patterns not validating properly #8157
authorGeoff Kimball <geoff@zurb.com>
Wed, 2 Mar 2016 01:21:10 +0000 (17:21 -0800)
committerGeoff Kimball <geoff@zurb.com>
Wed, 16 Mar 2016 16:42:35 +0000 (09:42 -0700)
js/foundation.abide.js
test/visual/abide/text.html [new file with mode: 0644]

index 685045bac8ddc8de8710b4dff92648f1a3e0fee1..4201d0b86869a5c42475a350554d2a3b861f6081 100644 (file)
@@ -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 (file)
index 0000000..f52d9eb
--- /dev/null
@@ -0,0 +1,41 @@
+<!doctype html>
+<!--[if IE 9]><html class="lt-ie10" lang="en" > <![endif]-->
+<html class="no-js" lang="en" dir="ltr">
+  <head>
+    <meta charset="utf-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <title>Foundation for Sites Testing</title>
+    <link href="../assets/css/foundation.css" rel="stylesheet" />
+  </head>
+  <body>
+    <div class="row column">
+      <h1>Abide: Text Fields</h1>
+
+      <p>This form has one required text field and one optional text field.</p>
+
+      <form data-abide novalidate>
+        <input required type="text" placeholder="Required">
+        <input type="text" placeholder="Not required">
+        <button type="submit" class="button">Submit</button>
+        <button type="reset" class="button">Reset</button>
+      </form>
+
+      <hr>
+
+      <p>This field uses a custom pattern. The field is valid if the input is between 4 and 16 characters long.</p>
+
+      <form data-abide novalidate>
+        <input required type="text" pattern="customPattern">
+        <button type="submit" class="button">Submit</button>
+        <button type="reset" class="button">Reset</button>
+      </form>
+    </div>
+
+    <script src="../assets/js/vendor.js"></script>
+    <script src="../assets/js/foundation.js"></script>
+    <script>
+      Foundation.Abide.defaults.patterns['customPattern'] = /^\w{4,16}$/;
+      $(document).foundation();
+    </script>
+  </body>
+</html>