Browse Source

options as attribs

Robin Herbots 11 years ago
parent
commit
0c4c5c676f

+ 15 - 1
README.md

@@ -49,7 +49,7 @@ $(document).ready(function(){
 });
 ```
 
-or
+or via data-inputmask attribute
 
 ```html
 <input data-inputmask="'alias': 'date'" />
@@ -62,6 +62,20 @@ $(document).ready(function(){
 });
 ```
 
+Any option can also be passed through the use of a data attribute. Use data-inputmask-***the name op the option***="value"
+
+```html
+<input id="example1" data-inputmask-clearmaskonlostfocus="false" />
+<input id="example2" data-inputmask-regex="[a-za-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?" />
+```
+```javascript
+$(document).ready(function(){
+   $(example1).inputmask("99-9999999");
+   $(selector).inputmask("Regex");
+});
+```
+
+
 #### Default masking definitions
 
   - 9 : numeric

+ 1 - 1
bower.json

@@ -1,6 +1,6 @@
 {
     "name": "jquery.inputmask",
-    "version": "3.0.2",
+    "version": "3.0.3",
     "main": "./dist/jquery.inputmask.bundle.js",
 	"keywords" : ["jQuery", "plugins", "input", "form", "inputmask", "mask"],
 	"description": "jquery.inputmask is a jquery plugin which create an input mask.",

+ 1 - 1
build.properties

@@ -7,7 +7,7 @@ distdir = dist
 
 build.major = 3
 build.minor = 0
-build.revision = 2
+build.revision = 3
 
 target = jquery.inputmask.bundle.js
 target.min = jquery.inputmask.bundle.min.js

BIN
dist/jQuery.InputMask.3.0.2.nupkg


BIN
dist/jQuery.InputMask.3.0.3.nupkg


+ 45 - 70
dist/jquery.inputmask.bundle.js

@@ -3,7 +3,7 @@
 * http://github.com/RobinHerbots/jquery.inputmask
 * Copyright (c) 2010 - 2014 Robin Herbots
 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-* Version: 3.0.2
+* Version: 3.0.3
 */
 
 (function ($) {
@@ -1666,6 +1666,15 @@
             }
         };
         $.fn.inputmask = function (fn, options) {
+            function importAttributeOptions(npt, opts) {
+                var $npt = $(npt);
+                for (var option in opts) {
+                    var optionData = $npt.data("inputmask-" + option.toLowerCase());
+                    if (optionData != undefined)
+                        opts[option] = optionData;
+                }
+                return opts;
+            }
             var opts = $.extend(true, {}, $.inputmask.defaults, options),
                 maskset;
 
@@ -1679,9 +1688,9 @@
 
                         return this.each(function () {
                             if ($.isArray(maskset)) {
-                                multiMaskScope(this, maskset, opts);
+                                multiMaskScope(this, maskset, importAttributeOptions(this, opts));
                             } else
-                                maskScope($.extend(true, {}, maskset), opts, { "action": "mask", "el": this });
+                                maskScope($.extend(true, {}, maskset), importAttributeOptions(this, opts), { "action": "mask", "el": this });
                         });
                     case "unmaskedvalue":
                         var $input = $(this), input = this;
@@ -1762,9 +1771,9 @@
                         if (maskset == undefined) { return this; }
                         return this.each(function () {
                             if ($.isArray(maskset)) {
-                                multiMaskScope(this, maskset, opts);
+                                multiMaskScope(this, maskset, importAttributeOptions(this, opts));
                             } else
-                                maskScope($.extend(true, {}, maskset), opts, { "action": "mask", "el": this });
+                                maskScope($.extend(true, {}, maskset), importAttributeOptions(this, opts), { "action": "mask", "el": this });
                         });
 
                         break;
@@ -1777,9 +1786,9 @@
                 if (maskset == undefined) { return this; }
                 return this.each(function () {
                     if ($.isArray(maskset)) {
-                        multiMaskScope(this, maskset, opts);
+                        multiMaskScope(this, maskset, importAttributeOptions(this, opts));
                     } else
-                        maskScope($.extend(true, {}, maskset), opts, { "action": "mask", "el": this });
+                        maskScope($.extend(true, {}, maskset), importAttributeOptions(this, opts), { "action": "mask", "el": this });
                 });
             } else if (fn == undefined) {
                 //look for data-inputmask atribute - the attribute should only contain optipns
@@ -1806,7 +1815,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2014 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 3.0.2
+Version: 3.0.3
 
 Optional extensions on the jquery.inputmask base
 */
@@ -1916,7 +1925,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2014 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 3.0.2
+Version: 3.0.3
 
 Optional extensions on the jquery.inputmask base
 */
@@ -2013,7 +2022,8 @@ Optional extensions on the jquery.inputmask base
                     cardinality: 2,
                     prevalidator: [{
                         validator: function (chrs, buffer, pos, strict, opts) {
-                            var isValid = opts.regex.val1pre.test(chrs);
+                            if (!isNaN(buffer[pos + 1])) chrs += buffer[pos + 1];
+                            var isValid = chrs.length == 1 ? opts.regex.val1pre.test(chrs) : opts.regex.val1.test(chrs);
                             if (!strict && !isValid) {
                                 isValid = opts.regex.val1.test("0" + chrs);
                                 if (isValid) {
@@ -2028,7 +2038,7 @@ Optional extensions on the jquery.inputmask base
                 },
                 '2': { //val2 ~ day or month
                     validator: function (chrs, buffer, pos, strict, opts) {
-                        var frontValue = buffer.join('').substr(0, 3);
+                        var frontValue = (opts.mask.indexOf("2") == opts.mask.length - 1) ? buffer.join('').substr(5, 3) : buffer.join('').substr(0, 3);
                         if (frontValue.indexOf(opts.placeholder[0]) != -1) frontValue = "01" + opts.separator;
                         var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
                         if (!strict && !isValid) {
@@ -2040,14 +2050,33 @@ Optional extensions on the jquery.inputmask base
                                 }
                             }
                         }
+
+                        //check leap yeap
+                        if ((opts.mask.indexOf("2") == opts.mask.length - 1) && isValid) {
+                            var dayMonthValue = buffer.join('').substr(4, 4) + chrs;
+                            if (dayMonthValue != opts.leapday)
+                                return true;
+                            else {
+                                var year = parseInt(buffer.join('').substr(0, 4), 10);  //detect leap year
+                                if (year % 4 === 0)
+                                    if (year % 100 === 0)
+                                        if (year % 400 === 0)
+                                            return true;
+                                        else return false;
+                                    else return true;
+                                else return false;
+                            }
+                        }
+
                         return isValid;
                     },
                     cardinality: 2,
                     prevalidator: [{
                         validator: function (chrs, buffer, pos, strict, opts) {
-                            var frontValue = buffer.join('').substr(0, 3);
+                            if (!isNaN(buffer[pos + 1])) chrs += buffer[pos + 1];
+                            var frontValue = (opts.mask.indexOf("2") == opts.mask.length - 1) ? buffer.join('').substr(5, 3) : buffer.join('').substr(0, 3);
                             if (frontValue.indexOf(opts.placeholder[0]) != -1) frontValue = "01" + opts.separator;
-                            var isValid = opts.regex.val2pre(opts.separator).test(frontValue + chrs);
+                            var isValid = chrs.length == 1 ? opts.regex.val2pre(opts.separator).test(frontValue + chrs) : opts.regex.val2(opts.separator).test(frontValue + chrs);
                             if (!strict && !isValid) {
                                 isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs);
                                 if (isValid) {
@@ -2182,60 +2211,6 @@ Optional extensions on the jquery.inputmask base
                     var today = new Date();
                     $input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString());
                 }
-            },
-            definitions: {
-                '2': { //val2 ~ day or month
-                    validator: function (chrs, buffer, pos, strict, opts) {
-                        var frontValue = buffer.join('').substr(5, 3);
-                        if (frontValue.indexOf(opts.placeholder[5]) != -1) frontValue = "01" + opts.separator;
-                        var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
-                        if (!strict && !isValid) {
-                            if (chrs.charAt(1) == opts.separator || "-./".indexOf(chrs.charAt(1)) != -1) {
-                                isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs.charAt(0));
-                                if (isValid) {
-                                    buffer[pos - 1] = "0";
-                                    return { "refreshFromBuffer": { start: pos - 1, end: pos }, "pos": pos, "c": chrs.charAt(0) };
-                                }
-                            }
-                        }
-
-                        //check leap yeap
-                        if (isValid) {
-                            var dayMonthValue = buffer.join('').substr(4, 4) + chrs;
-                            if (dayMonthValue != opts.leapday)
-                                return true;
-                            else {
-                                var year = parseInt(buffer.join('').substr(0, 4), 10);  //detect leap year
-                                if (year % 4 === 0)
-                                    if (year % 100 === 0)
-                                        if (year % 400 === 0)
-                                            return true;
-                                        else return false;
-                                    else return true;
-                                else return false;
-                            }
-                        }
-
-                        return isValid;
-                    },
-                    cardinality: 2,
-                    prevalidator: [{
-                        validator: function (chrs, buffer, pos, strict, opts) {
-                            var frontValue = buffer.join('').substr(5, 3);
-                            if (frontValue.indexOf(opts.placeholder[5]) != -1) frontValue = "01" + opts.separator;
-                            var isValid = opts.regex.val2pre(opts.separator).test(frontValue + chrs);
-                            if (!strict && !isValid) {
-                                isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs);
-                                if (isValid) {
-                                    buffer[pos] = "0";
-                                    pos++;
-                                    return { "pos": pos };
-                                }
-                            }
-                            return isValid;
-                        }, cardinality: 1
-                    }]
-                }
             }
         },
         'dd.mm.yyyy': {
@@ -2413,7 +2388,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2014 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 3.0.2
+Version: 3.0.3
 
 Optional extensions on the jquery.inputmask base
 */
@@ -2580,7 +2555,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2014 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 3.0.2
+Version: 3.0.3
 
 Regex extensions on the jquery.inputmask base
 Allows for using regular expressions as a mask
@@ -2767,7 +2742,7 @@ Input Mask plugin extensions
 http://github.com/RobinHerbots/jquery.inputmask
 Copyright (c) 2010 - 2014 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 3.0.2
+Version: 3.0.3
 
 Phone extension.
 When using this extension make sure you specify the correct url to get the masks

File diff suppressed because it is too large
+ 88 - 89
dist/jquery.inputmask.bundle.min.js


File diff suppressed because it is too large
+ 16 - 17
dist/min/jquery.inputmask.date.extensions.js


File diff suppressed because it is too large
+ 57 - 56
dist/min/jquery.inputmask.js


+ 1 - 1
jquery.inputmask.jquery.json

@@ -8,7 +8,7 @@
 		"inputmask",
 		"mask"
     ],
-    "version": "3.0.2",
+    "version": "3.0.3",
     "author": {
         "name": "Robin Herbots",
         "url": "http://github.com/RobinHerbots/jquery.inputmask"

+ 24 - 58
js/jquery.inputmask.date.extensions.js

@@ -100,7 +100,8 @@ Optional extensions on the jquery.inputmask base
                     cardinality: 2,
                     prevalidator: [{
                         validator: function (chrs, buffer, pos, strict, opts) {
-                            var isValid = opts.regex.val1pre.test(chrs);
+                            if (!isNaN(buffer[pos + 1])) chrs += buffer[pos + 1];
+                            var isValid = chrs.length == 1 ? opts.regex.val1pre.test(chrs) : opts.regex.val1.test(chrs);
                             if (!strict && !isValid) {
                                 isValid = opts.regex.val1.test("0" + chrs);
                                 if (isValid) {
@@ -115,7 +116,7 @@ Optional extensions on the jquery.inputmask base
                 },
                 '2': { //val2 ~ day or month
                     validator: function (chrs, buffer, pos, strict, opts) {
-                        var frontValue = buffer.join('').substr(0, 3);
+                        var frontValue = (opts.mask.indexOf("2") == opts.mask.length - 1) ? buffer.join('').substr(5, 3) : buffer.join('').substr(0, 3);
                         if (frontValue.indexOf(opts.placeholder[0]) != -1) frontValue = "01" + opts.separator;
                         var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
                         if (!strict && !isValid) {
@@ -127,14 +128,33 @@ Optional extensions on the jquery.inputmask base
                                 }
                             }
                         }
+
+                        //check leap yeap
+                        if ((opts.mask.indexOf("2") == opts.mask.length - 1) && isValid) {
+                            var dayMonthValue = buffer.join('').substr(4, 4) + chrs;
+                            if (dayMonthValue != opts.leapday)
+                                return true;
+                            else {
+                                var year = parseInt(buffer.join('').substr(0, 4), 10);  //detect leap year
+                                if (year % 4 === 0)
+                                    if (year % 100 === 0)
+                                        if (year % 400 === 0)
+                                            return true;
+                                        else return false;
+                                    else return true;
+                                else return false;
+                            }
+                        }
+
                         return isValid;
                     },
                     cardinality: 2,
                     prevalidator: [{
                         validator: function (chrs, buffer, pos, strict, opts) {
-                            var frontValue = buffer.join('').substr(0, 3);
+                            if (!isNaN(buffer[pos + 1])) chrs += buffer[pos + 1];
+                            var frontValue = (opts.mask.indexOf("2") == opts.mask.length - 1) ? buffer.join('').substr(5, 3) : buffer.join('').substr(0, 3);
                             if (frontValue.indexOf(opts.placeholder[0]) != -1) frontValue = "01" + opts.separator;
-                            var isValid = opts.regex.val2pre(opts.separator).test(frontValue + chrs);
+                            var isValid = chrs.length == 1 ? opts.regex.val2pre(opts.separator).test(frontValue + chrs) : opts.regex.val2(opts.separator).test(frontValue + chrs);
                             if (!strict && !isValid) {
                                 isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs);
                                 if (isValid) {
@@ -269,60 +289,6 @@ Optional extensions on the jquery.inputmask base
                     var today = new Date();
                     $input.val(today.getFullYear().toString() + (today.getMonth() + 1).toString() + today.getDate().toString());
                 }
-            },
-            definitions: {
-                '2': { //val2 ~ day or month
-                    validator: function (chrs, buffer, pos, strict, opts) {
-                        var frontValue = buffer.join('').substr(5, 3);
-                        if (frontValue.indexOf(opts.placeholder[5]) != -1) frontValue = "01" + opts.separator;
-                        var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
-                        if (!strict && !isValid) {
-                            if (chrs.charAt(1) == opts.separator || "-./".indexOf(chrs.charAt(1)) != -1) {
-                                isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs.charAt(0));
-                                if (isValid) {
-                                    buffer[pos - 1] = "0";
-                                    return { "refreshFromBuffer": { start: pos - 1, end: pos }, "pos": pos, "c": chrs.charAt(0) };
-                                }
-                            }
-                        }
-
-                        //check leap yeap
-                        if (isValid) {
-                            var dayMonthValue = buffer.join('').substr(4, 4) + chrs;
-                            if (dayMonthValue != opts.leapday)
-                                return true;
-                            else {
-                                var year = parseInt(buffer.join('').substr(0, 4), 10);  //detect leap year
-                                if (year % 4 === 0)
-                                    if (year % 100 === 0)
-                                        if (year % 400 === 0)
-                                            return true;
-                                        else return false;
-                                    else return true;
-                                else return false;
-                            }
-                        }
-
-                        return isValid;
-                    },
-                    cardinality: 2,
-                    prevalidator: [{
-                        validator: function (chrs, buffer, pos, strict, opts) {
-                            var frontValue = buffer.join('').substr(5, 3);
-                            if (frontValue.indexOf(opts.placeholder[5]) != -1) frontValue = "01" + opts.separator;
-                            var isValid = opts.regex.val2pre(opts.separator).test(frontValue + chrs);
-                            if (!strict && !isValid) {
-                                isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs);
-                                if (isValid) {
-                                    buffer[pos] = "0";
-                                    pos++;
-                                    return { "pos": pos };
-                                }
-                            }
-                            return isValid;
-                        }, cardinality: 1
-                    }]
-                }
             }
         },
         'dd.mm.yyyy': {

+ 15 - 6
js/jquery.inputmask.js

@@ -1666,6 +1666,15 @@
             }
         };
         $.fn.inputmask = function (fn, options) {
+            function importAttributeOptions(npt, opts) {
+                var $npt = $(npt);
+                for (var option in opts) {
+                    var optionData = $npt.data("inputmask-" + option.toLowerCase());
+                    if (optionData != undefined)
+                        opts[option] = optionData;
+                }
+                return opts;
+            }
             var opts = $.extend(true, {}, $.inputmask.defaults, options),
                 maskset;
 
@@ -1679,9 +1688,9 @@
 
                         return this.each(function () {
                             if ($.isArray(maskset)) {
-                                multiMaskScope(this, maskset, opts);
+                                multiMaskScope(this, maskset, importAttributeOptions(this, opts));
                             } else
-                                maskScope($.extend(true, {}, maskset), opts, { "action": "mask", "el": this });
+                                maskScope($.extend(true, {}, maskset), importAttributeOptions(this, opts), { "action": "mask", "el": this });
                         });
                     case "unmaskedvalue":
                         var $input = $(this), input = this;
@@ -1762,9 +1771,9 @@
                         if (maskset == undefined) { return this; }
                         return this.each(function () {
                             if ($.isArray(maskset)) {
-                                multiMaskScope(this, maskset, opts);
+                                multiMaskScope(this, maskset, importAttributeOptions(this, opts));
                             } else
-                                maskScope($.extend(true, {}, maskset), opts, { "action": "mask", "el": this });
+                                maskScope($.extend(true, {}, maskset), importAttributeOptions(this, opts), { "action": "mask", "el": this });
                         });
 
                         break;
@@ -1777,9 +1786,9 @@
                 if (maskset == undefined) { return this; }
                 return this.each(function () {
                     if ($.isArray(maskset)) {
-                        multiMaskScope(this, maskset, opts);
+                        multiMaskScope(this, maskset, importAttributeOptions(this, opts));
                     } else
-                        maskScope($.extend(true, {}, maskset), opts, { "action": "mask", "el": this });
+                        maskScope($.extend(true, {}, maskset), importAttributeOptions(this, opts), { "action": "mask", "el": this });
                 });
             } else if (fn == undefined) {
                 //look for data-inputmask atribute - the attribute should only contain optipns