浏览代码

Merge branch 'dev'

Robin Herbots 13 年之前
父节点
当前提交
8fe24babc6
共有 3 个文件被更改,包括 140 次插入22 次删除
  1. 53 3
      README.md
  2. 68 7
      js/jquery.inputmask.extentions.js
  3. 19 12
      js/jquery.inputmask.js

+ 53 - 3
README.md

@@ -46,7 +46,7 @@ Example of some new definitions:
 insertMode: false
 ```
 
-These allow for a finer date validation then 99/99/9999 which also allows 33/33/3333 for example.  
+These allow for a finer date validation then 99/99/9999 which also allows 33/33/3333 for example. 
 In the jquery.inputmask.extentions.js you find a full date input validation which takes days, months & leap years into account.
 
 Also extra features like mask-repetitions (greedy and non-gready) and many other additions are included.  In the examples you will find more about them.
@@ -270,7 +270,16 @@ $(document).ready(function(){
 });
 ```
 
-### rtl input
+### onKeyUp / onKeyDown option
+
+Use this to do some extra processing of the input when certain keys are pressed.
+This can be usefull when implementing an alias, ex. decimal alias, autofill the digits when pressing tab.
+
+see jquery.inputmask.extentions.js for some examples
+
+
+## Markup options
+### RTL input 
 
 Just add the dir="rtl" attribute to the input element
 
@@ -278,7 +287,48 @@ Just add the dir="rtl" attribute to the input element
 <input id="test" dir="rtl" />
 ```
 
+
 ## Compiling with Google Closure Compiler
 
 First grab the sources from github.  In the root you type ant.
-A new folder dist is created with the minified and optimized js-files
+A new folder dist is created with the minified and optimized js-files
+
+# jquery.inputmask extentions
+
+## Alias definitions
+
+### date aliases
+
+```javascript
+$(document).ready(function(){
+   $("#date").inputmask("dd/mm/yyyy");
+   $("#date").inputmask("mm/dd/yyyy");
+   $("#date").inputmask("date"); // alias for dd/mm/yyyy
+});
+```
+
+The date aliases take leapyears into account.  There is also autocompletion on day, month, year.
+For example:
+
+input:	2/2/2012 		result: 02/02/2012
+input:  352012			result: 03/05/2012
+input:  3530			result: 03/05/2030
+input: <ctrl> rightarrow	result: the date from today
+
+### numeric aliases
+
+```javascript
+$(document).ready(function(){
+   $("#numeric").inputmask("decimal");
+   $("#numeric").inputmask("non-negative-decimal");
+   $("#numeric").inputmask("integer"); 
+});
+```
+
+Autocompletion on tab with decimal numbers
+
+```javascript
+$(document).ready(function(){
+   $("#numeric").inputmask("decimal" { digits: 3 });
+});
+```

+ 68 - 7
js/jquery.inputmask.extentions.js

@@ -1,9 +1,9 @@
 /*
 Input Mask plugin extentions
 http://github.com/RobinHerbots/jquery.inputmask
-Copyright (c) 2010 Robin Herbots
+Copyright (c) 2010 - 2012 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 0.0.6
+Version: 1.0.0
 
 Optional extentions on the jquery.inputmask base
 */
@@ -55,10 +55,20 @@ Optional extentions on the jquery.inputmask base
             regex: {
                 monthpre: new RegExp("[01]"),
                 month: new RegExp("((0[1-9]|[12][0-9])\/(0[1-9]|1[012]))|(30\/(0[13-9]|1[012]))|(31\/(0[13578]|1[02]))"),
+				yearpre1: new RegExp("[12]"),                
+				yearpre3: new RegExp("(19|20)\\d"),
                 year: new RegExp("(19|20)\\d{2}"),
                 daypre: new RegExp("[0-3]"),
                 day: new RegExp("0[1-9]|[12][0-9]|3[01]")
             },
+            leapday: "29/02/",
+            onKeyUp: function(e, opts) {
+                                var $input = $(this), input = this;
+                                if(e.ctrlKey && e.keyCode == opts.keyCode.RIGHT){
+                                	var today = new Date();
+                                	$input.val(today.getDate().toString() + (today.getMonth()+1).toString() + today.getFullYear().toString());
+                                }
+                            },
             definitions: {
                 'd': { //day
                     validator: function(chrs, buffer, pos, strict, opts) {
@@ -126,7 +136,7 @@ Optional extentions on the jquery.inputmask base
                             validator: function(chrs, buffer, pos, strict, opts) {
                                 if (opts.regex.year.test(chrs)) {
                                     var dayMonthValue = buffer.join('').substr(0, 6);
-                                    if (dayMonthValue != "29/02/")
+                                    if (dayMonthValue != opts.leapday)
                                         return true;
                                     else {
                                         var year = parseInt(chrs);  //detect leap year
@@ -142,7 +152,21 @@ Optional extentions on the jquery.inputmask base
                             },
                             cardinality: 4,
                             prevalidator: [
-                        { validator: "[12]", cardinality: 1 },
+                        { validator: function(chrs, buffer, pos, strict, opts) {
+                        		var isValid = opts.regex.yearpre1.test(chrs);
+                            	if (!strict && !isValid) {
+                                	var yearPrefix = (new Date()).getFullYear().toString().slice(0,2);
+
+                            	    isValid = opts.regex.yearpre3.test(yearPrefix + chrs);
+                        	        if (isValid) {
+                    	                buffer[pos++] = yearPrefix[0];
+                	                    buffer[pos++] = yearPrefix[1];
+            	                        return pos;
+        	                        }
+    	                        }
+	                            return isValid;
+                        	}
+                        	, cardinality: 1 },
                         { validator: "(19|20)", cardinality: 2 },
                         { validator: "(19|20)\\d", cardinality: 3 }
                         ]
@@ -159,8 +183,18 @@ Optional extentions on the jquery.inputmask base
                         daypre: new RegExp("((0[13-9]|1[012])\/[0-3])|(02\/[0-2])"),
                         month: new RegExp("0[1-9]|1[012]"),
                         monthpre: new RegExp("[01]"),
+                        yearpre1: new RegExp("[12]"),
+                        yearpre3: new RegExp("(19|20)\\d"),                 
                         year: new RegExp("(19|20)\\d{2}")
                     },
+                    leapday: "02/29/",
+                    onKeyUp: function(e, opts) {
+                                var $input = $(this), input = this;
+                                if(e.ctrlKey && e.keyCode == opts.keyCode.RIGHT){
+                                	var today = new Date();
+                                	$input.val((today.getMonth()+1).toString() + today.getDate().toString() + today.getFullYear().toString());
+                                }
+                            },
                     definitions: {
                         'd': { //day
                             validator: function(chrs, buffer, pos, strict, opts) {
@@ -229,7 +263,7 @@ Optional extentions on the jquery.inputmask base
                                     validator: function(chrs, buffer, pos, strict, opts) {
                                         if (opts.regex.year.test(chrs)) {
                                             var monthDayValue = buffer.join('').substr(0, 6);
-                                            if (monthDayValue != "02/29/")
+                                            if (monthDayValue != opts.leapday)
                                                 return true;
                                             else {
                                                 var year = parseInt(chrs);  //detect leap year
@@ -245,7 +279,21 @@ Optional extentions on the jquery.inputmask base
                                     },
                                     cardinality: 4,
                                     prevalidator: [
-                        { validator: "[12]", cardinality: 1 },
+                        { validator: function(chrs, buffer, pos, strict, opts) {
+                        		var isValid = opts.regex.yearpre1.test(chrs);
+                            	if (!strict && !isValid) {
+                                	var yearPrefix = (new Date()).getFullYear().toString().slice(0,2);
+
+                            	    isValid = opts.regex.yearpre3.test(yearPrefix + chrs);
+                        	        if (isValid) {
+                    	                buffer[pos++] = yearPrefix[0];
+                	                    buffer[pos++] = yearPrefix[1];
+            	                        return pos;
+        	                        }
+    	                        }
+	                            return isValid;
+                        	}                       
+                        , cardinality: 1 },
                         { validator: "(19|20)", cardinality: 2 },
                         { validator: "(19|20)\\d", cardinality: 3 }
                         ]
@@ -286,7 +334,20 @@ Optional extentions on the jquery.inputmask base
                             greedy: false,
                             numericInput: true,
                             regex: {
-                            number: function(radixPoint, digits) { return new RegExp("^[\+\\d\-]{1}\\d*[" + radixPoint + "]?\\d" + digits + "$"); }
+                                number: function(radixPoint, digits) { return new RegExp("^[\+\\d\-]{1}\\d*[" + radixPoint + "]?\\d" + digits + "$"); }
+                            },
+                            onKeyDown: function(e, opts) {
+                                var $input = $(this), input = this;
+                                if (e.keyCode == opts.keyCode.TAB) {
+                                    var nptStr = input._valueGet();
+                                    var radixPosition = nptStr.indexOf(opts.radixPoint[opts.radixPoint.length -1]);
+                                    if(radixPosition != -1){
+                                    	for(var i = 1; i < opts.digits; i++) {
+                                    		if(nptStr[radixPosition + i]) nptStr = nptStr + "0";  
+                                    	}
+                                    	$input.val(nptStr);
+                                    }
+                                }
                             },
                             definitions: {
                                 '~': { //real number

+ 19 - 12
js/jquery.inputmask.js

@@ -1,9 +1,9 @@
 /*
 Input Mask plugin for jquery
 http://github.com/RobinHerbots/jquery.inputmask
-Copyright (c) 2010 Robin Herbots
+Copyright (c) 2010 - 2012 Robin Herbots
 Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
-Version: 0.6.0a
+Version: 1.0.0
  
 This plugin is based on the masked input plugin written by Josh Bush (digitalbush.com)
 */
@@ -31,6 +31,8 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
                 insertMode: true, //insert the input or overwrite the input
                 clearIncomplete: false, //clear the incomplete input on blur
                 aliases: {}, //aliases definitions => see jquery.inputmask.extentions.js
+                onKeyUp: $.noop, //override to implement autocomplete on certain keys for example
+                onKeyDown: $.noop, //override to implement autocomplete on certain keys for example
                 definitions: {
                     '9': {
                         validator: "[0-9]",
@@ -591,16 +593,8 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
                     }, 0);
                 }).bind("keydown.inputmask", keydownEvent
                 ).bind("keypress.inputmask", keypressEvent
-                ).bind("keyup.inputmask", function(e) {
-                    var $input = $(this), input = this;
-                    var k = e.keyCode;
-                    if (k == opts.keyCode.TAB && $input.hasClass('focus.inputmask') && input._valueGet().length == 0) {
-                        buffer = _buffer.slice();
-                        writeBuffer(input, buffer);
-                        if (!isRTL) caret(input, 0);
-                        undoBuffer = input._valueGet();
-                    }
-                }).bind(pasteEventName, function() {
+                ).bind("keyup.inputmask", keyupEvent
+                ).bind(pasteEventName, function() {
                     var input = this;
                     setTimeout(function() {
                         caret(input, checkVal(input, buffer, true));
@@ -816,6 +810,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
                         }
                     }
 
+					opts.onKeyDown.call(this, e, opts); //extra stuff todo on keydown
                     ignorable = $.inArray(k, opts.ignorables) != -1;
                 }
 
@@ -866,6 +861,18 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
                         }
                     }
                 }
+
+                function keyupEvent(e) {
+                    var $input = $(this), input = this;
+                    var k = e.keyCode;
+                    opts.onKeyUp.call(this, e, opts); //extra stuff todo on keyup
+                    if (k == opts.keyCode.TAB && $input.hasClass('focus.inputmask') && input._valueGet().length == 0) {
+                        buffer = _buffer.slice();
+                        writeBuffer(input, buffer);
+                        if (!isRTL) caret(input, 0);
+                        undoBuffer = input._valueGet();
+                    }
+                }
             }
         };
     }