Browse Source

add $.inputmask.isValid function #310

Robin Herbots 12 years ago
parent
commit
107407ab98
3 changed files with 41 additions and 1 deletions
  1. 8 0
      README.md
  2. 22 1
      js/jquery.inputmask.js
  3. 11 0
      qunit/tests.js

+ 8 - 0
README.md

@@ -591,6 +591,14 @@ Think of formatting values to show in jqGrid or on other elements then inputs.
 var formattedDate = $.inputmask.format("2331973", { alias: "dd/mm/yyyy"});
 ```
 
+## Value validating
+
+Validate the input a given input against the mask.
+
+```javascript
+var isValid = $.inputmask.isValid("2331973", { alias: "dd/mm/yyyy"});
+```
+
 ## Compiling with Google Closure Compiler
 
 First grab the sources from GitHub.  In the root you type ant.

+ 22 - 1
js/jquery.inputmask.js

@@ -114,6 +114,7 @@
                                 openenings[openenings.length - 1]["matches"].push(quantifier);
                             } else {
                                 currentToken.matches.push(quantifier);
+                                maskTokens.push(currentToken);
                                 currentToken = new maskToken();
                             }
                             break;
@@ -1545,8 +1546,23 @@
                             isRTL = true;
                         }
 
-                        checkVal($el, false, false, actionObj["value"].split(''), true);
+                        checkVal($el, false, false, actionObj["value"].split(''));
                         return getActiveBuffer().join('');
+                    case "isValid":
+                        $el = $({});
+                        $el.data('_inputmask', {
+                            'masksets': masksets,
+                            'activeMasksetIndex': activeMasksetIndex,
+                            'opts': opts,
+                            'isRTL': opts.numericInput
+                        });
+                        if (opts.numericInput) {
+                            opts.isNumeric = opts.numericInput;
+                            isRTL = true;
+                        }
+
+                        checkVal($el, false, true, actionObj["value"].split(''));
+                        return isComplete(getActiveBuffer());
                 }
             }
         };
@@ -1626,6 +1642,11 @@
                 var opts = $.extend(true, {}, $.inputmask.defaults, options);
                 resolveAlias(opts.alias, options, opts);
                 return maskScope(generateMaskSets(opts), 0, opts, { "action": "format", "value": value });
+            },
+            isValid: function (value, options) {
+                var opts = $.extend(true, {}, $.inputmask.defaults, options);
+                resolveAlias(opts.alias, options, opts);
+                return maskScope(generateMaskSets(opts), 0, opts, { "action": "isValid", "value": value });
             }
         };
         $.fn.inputmask = function (fn, options) {

+ 11 - 0
qunit/tests.js

@@ -1824,6 +1824,17 @@ test("$.inputmask.format(\"016501030020001DE1015170\", { mask: \"99 999 999 999
     equal(formattedValue, "01 650 103 002 0001 DE101 5170", "Result " + formattedValue);
 });
 
+module("Value Validating");
+test("$.inputmask.isValid(\"23/03/1973\", { alias: \"date\"})", function () {
+    var formattedValue =$.inputmask.isValid("23/03/1973", { alias: "date"});
+    equal(formattedValue, true, "Result " + formattedValue);
+});
+
+test("$.inputmask.isValid(\"01 650 103 002 0001 DE101 5170\", { mask: \"99 999 999 999 9999 \\D\\E*** 9999\"})", function () {
+    var formattedValue =$.inputmask.isValid("01 650 103 002 0001 DE101 5170", { mask: "99 999 999 999 9999 \\D\\E*** 9999"});
+    equal(formattedValue, true, "Result " + formattedValue);
+});
+
 module("Dynamic Masks");
 test("inputmask(\"*{1,20}@*{1,20}.*{2,6}[.*{2}]\" - email mask", function () {
     $('body').append('<input type="text" id="testmask" />');