From 3a80bec910534b0c0cc4840414ee296b49b24767 Mon Sep 17 00:00:00 2001 From: Jorge Plaza Date: Tue, 26 Aug 2014 01:30:02 -0430 Subject: [PATCH 01/12] Decimal places configuration. Several fixes: Callback function is not called when a negative numbers is introduced, removeNumeric removes listeners for keyUp event. Test page improved, with all examples working as expected. --- numeric/jquery.numeric.js | 36 +++++++++++++++++++++++++++++++----- numeric/test.html | 12 ++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index 96e800f..de699c1 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -22,6 +22,7 @@ * @example $(".numeric").numeric(","); // use , as separator * @example $(".numeric").numeric({ decimal : "," }); // use , as separator * @example $(".numeric").numeric({ negative : false }); // do not allow negative values + * @example $(".numeric").numeric({ decimalPlaces : 2 }); // only allow 2 decimal places * @example $(".numeric").numeric(null, callback); // use default values, pass on the 'callback' function * */ @@ -29,7 +30,7 @@ $.fn.numeric = function(config, callback) { if(typeof config === 'boolean') { - config = { decimal: config }; + config = { decimal: config, negative: true, decimalPlaces: -1 }; } config = config || {}; // if config.negative undefined, set to true (default is to allow negative numbers) @@ -38,10 +39,12 @@ $.fn.numeric = function(config, callback) var decimal = (config.decimal === false) ? "" : config.decimal || "."; // allow negatives var negative = (config.negative === true) ? true : false; + // set decimal places + var decimalPlaces = (typeof config.decimalPlaces == "undefined") ? -1 : config.decimalPlaces; // callback function callback = (typeof(callback) == "function" ? callback : function() {}); // set data and methods - return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.callback", callback).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur); + return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.callback", callback).data("numeric.decimalPlaces", decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur); }; $.fn.numeric.keypress = function(e) @@ -49,6 +52,7 @@ $.fn.numeric.keypress = function(e) // get decimal character and determine if negatives are allowed var decimal = $.data(this, "numeric.decimal"); var negative = $.data(this, "numeric.negative"); + var decimalPlaces = $.data(this, "numeric.decimalPlaces"); // get the key that was pressed var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0; // allow enter/return key (only when in an input box) @@ -133,6 +137,15 @@ $.fn.numeric.keypress = function(e) else { allow = true; + // remove extra decimal places + if(decimal && decimalPlaces > 0) + { + var dot = $.inArray(decimal, $(this).val().split('')); + if (dot >= 0 && $(this).val().length > dot + decimalPlaces) { + allow = false; + } + } + } return allow; }; @@ -148,6 +161,7 @@ $.fn.numeric.keyup = function(e) // get decimal character and determine if negatives are allowed var decimal = $.data(this, "numeric.decimal"); var negative = $.data(this, "numeric.negative"); + var decimalPlaces = $.data(this, "numeric.decimalPlaces"); // prepend a 0 if necessary if(decimal !== "" && decimal !== null) @@ -220,6 +234,17 @@ $.fn.numeric.keyup = function(e) } } } + + // remove extra decimal places + if(decimal && decimalPlaces > 0) + { + var dot = $.inArray(decimal, val.split('')); + if (dot >= 0) + { + val = val.substring(0, dot + decimalPlaces + 1); + selectionEnd = Math.min(val.length, selectionEnd); + } + } // set the value and prevent the cursor moving to the end this.value = val; $.fn.setSelection(this, [carat, selectionEnd]); @@ -230,10 +255,11 @@ $.fn.numeric.blur = function() { var decimal = $.data(this, "numeric.decimal"); var callback = $.data(this, "numeric.callback"); + var negative = $.data(this, "numeric.negative"); var val = this.value; - if(val !== "") + if(val !== "" && decimal) { - var re = new RegExp("^\\d+$|^\\d*" + decimal + "\\d+$"); + var re = new RegExp(negative?"-?":"" + "^\\d+$|^\\d*" + decimal + "\\d+$"); if(!re.exec(val)) { callback.apply(this); @@ -243,7 +269,7 @@ $.fn.numeric.blur = function() $.fn.removeNumeric = function() { - return this.data("numeric.decimal", null).data("numeric.negative", null).data("numeric.callback", null).unbind("keypress", $.fn.numeric.keypress).unbind("blur", $.fn.numeric.blur); + return this.data("numeric.decimal", null).data("numeric.negative", null).data("numeric.callback", null).data("numeric.decimalPlaces", null).unbind("keypress", $.fn.numeric.keypress).unbind("keyup", $.fn.numeric.keyup).unbind("blur", $.fn.numeric.blur); }; // Based on code from http://javascript.nwbox.com/cursor_position/ (Diego Perini ) diff --git a/numeric/test.html b/numeric/test.html index 631dbfc..54fe2df 100644 --- a/numeric/test.html +++ b/numeric/test.html @@ -8,12 +8,19 @@
Numbers only: +

Integers only: +

No negative values: +

No negative values (integer only): +

+ Numbers with up to 2 decimal places: + +

Remove numeric
- \ No newline at end of file + From 2a58fd0ae2c9204637def3b36c2b295183768938 Mon Sep 17 00:00:00 2001 From: Jorge Plaza Date: Tue, 26 Aug 2014 01:37:03 -0430 Subject: [PATCH 02/12] Removing wrong condition --- numeric/jquery.numeric.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index de699c1..07321ec 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -257,7 +257,7 @@ $.fn.numeric.blur = function() var callback = $.data(this, "numeric.callback"); var negative = $.data(this, "numeric.negative"); var val = this.value; - if(val !== "" && decimal) + if(val !== "") { var re = new RegExp(negative?"-?":"" + "^\\d+$|^\\d*" + decimal + "\\d+$"); if(!re.exec(val)) From d44f1f0489ce257d2f4b90e1f1b8bfbdec5542e4 Mon Sep 17 00:00:00 2001 From: ryanss Date: Tue, 2 Sep 2014 16:00:39 -0400 Subject: [PATCH 03/12] Fixes IE11 error and misc selection issues `getSelectionEnd` function was causing error "Unable to get property 'createRange' of undefined or null reference" in IE11 due to using the depreciated `document.selection` object. A previous patch attempted to fix this but it only modified `getSelectionStart` and created new bugs. When text was selected starting in the middle of the string and a non-numeric key was pressed in some cases the selection would jump around incorrectly and not stay the same as it should. This bug would also sometimes select characters when typing in the middle of a number when nothing was selected before the keypress. This patch fixes both of these issues. --- numeric/jquery.numeric.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index 96e800f..40cfa01 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -252,17 +252,10 @@ $.fn.getSelectionStart = function(o) if(o.type === "number"){ return undefined; } - else if (o.createTextRange) + else if (o.createTextRange && document.selection) { - var r; - if(typeof document.selection == "undefined") { - //On IE < 9 && IE >= 11 : "document.selection" is deprecated and you should use "document.getSelection()" - //https://github.com/SamWM/jQuery-Plugins/issues/62 - r = document.getSelection(); - } else { - r = document.selection.createRange().duplicate(); - r.moveEnd('character', o.value.length); - } + var r = document.selection.createRange().duplicate(); + r.moveEnd('character', o.value.length); if (r.text == '') return o.value.length; return o.value.lastIndexOf(r.text); @@ -278,7 +271,7 @@ $.fn.getSelectionEnd = function(o) if(o.type === "number"){ return undefined; } - else if (o.createTextRange) { + else if (o.createTextRange && document.selection) { var r = document.selection.createRange().duplicate() r.moveStart('character', -o.value.length) return r.text.length @@ -301,7 +294,7 @@ $.fn.setSelection = function(o, p) var r = o.createTextRange(); r.collapse(true); r.moveStart('character', p[0]); - r.moveEnd('character', p[1]); + r.moveEnd('character', p[1] - p[0]); r.select(); } else { From 75b9873cce96ad94c45aa8d49e2ab90aeede98fb Mon Sep 17 00:00:00 2001 From: ryanss Date: Wed, 3 Sep 2014 13:30:39 -0400 Subject: [PATCH 04/12] Fix small selection bug in IE8 When filling in an input box via jQuery/javascript that is currently focused `getSelectionStart` returns -1 which causes the first character of the new data in the input box to become selected. This patch does not allow the function to return less than 0 which fixes this somewhat superficial bug. This was only a bug in IE8; IE7 and IE9 worked as expected. --- numeric/jquery.numeric.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index c2427de..cb711aa 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -284,7 +284,7 @@ $.fn.getSelectionStart = function(o) r.moveEnd('character', o.value.length); if (r.text == '') return o.value.length; - return o.value.lastIndexOf(r.text); + return Math.max(0, o.value.lastIndexOf(r.text)); } else { try { return o.selectionStart; } catch(e) { return 0; } From 9b4d8d3ead5a5809308b7058d8152615e7f5aa73 Mon Sep 17 00:00:00 2001 From: ryanss Date: Wed, 17 Sep 2014 22:35:28 -0400 Subject: [PATCH 05/12] Add minified jquery.numeric file and bump version --- numeric/jquery.numeric.js | 8 ++++---- numeric/jquery.numeric.min.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 numeric/jquery.numeric.min.js diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index cb711aa..f7b63e2 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -3,8 +3,8 @@ * Copyright (c) 2006-2014 Sam Collett (http://www.texotela.co.uk) * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses. - * - * Version 1.4 + * + * Version 1.4.1 * Demo: http://www.texotela.co.uk/code/jquery/numeric/ * */ @@ -162,7 +162,7 @@ $.fn.numeric.keyup = function(e) var decimal = $.data(this, "numeric.decimal"); var negative = $.data(this, "numeric.negative"); var decimalPlaces = $.data(this, "numeric.decimalPlaces"); - + // prepend a 0 if necessary if(decimal !== "" && decimal !== null) { @@ -184,7 +184,7 @@ $.fn.numeric.keyup = function(e) } val = this.value; } - + // if pasted in, only allow the following characters var validChars = [0,1,2,3,4,5,6,7,8,9,'-',decimal]; // get length of the value (to loop through) diff --git a/numeric/jquery.numeric.min.js b/numeric/jquery.numeric.min.js new file mode 100644 index 0000000..dd0bb58 --- /dev/null +++ b/numeric/jquery.numeric.min.js @@ -0,0 +1,11 @@ +/* + * + * Copyright (c) 2006-2014 Sam Collett (http://www.texotela.co.uk) + * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) + * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses. + * + * Version 1.4.1 + * Demo: http://www.texotela.co.uk/code/jquery/numeric/ + * + */ +(function($){$.fn.numeric=function(config,callback){if(typeof config==="boolean"){config={decimal:config,negative:true,decimalPlaces:-1}}config=config||{};if(typeof config.negative=="undefined"){config.negative=true}var decimal=config.decimal===false?"":config.decimal||".";var negative=config.negative===true?true:false;var decimalPlaces=typeof config.decimalPlaces=="undefined"?-1:config.decimalPlaces;callback=typeof callback=="function"?callback:function(){};return this.data("numeric.decimal",decimal).data("numeric.negative",negative).data("numeric.callback",callback).data("numeric.decimalPlaces",decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur)};$.fn.numeric.keypress=function(e){var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");var key=e.charCode?e.charCode:e.keyCode?e.keyCode:0;if(key==13&&this.nodeName.toLowerCase()=="input"){return true}else if(key==13){return false}var allow=false;if(e.ctrlKey&&key==97||e.ctrlKey&&key==65){return true}if(e.ctrlKey&&key==120||e.ctrlKey&&key==88){return true}if(e.ctrlKey&&key==99||e.ctrlKey&&key==67){return true}if(e.ctrlKey&&key==122||e.ctrlKey&&key==90){return true}if(e.ctrlKey&&key==118||e.ctrlKey&&key==86||e.shiftKey&&key==45){return true}if(key<48||key>57){var value=$(this).val();if($.inArray("-",value.split(""))!==0&&negative&&key==45&&(value.length===0||parseInt($.fn.getSelectionStart(this),10)===0)){return true}if(decimal&&key==decimal.charCodeAt(0)&&$.inArray(decimal,value.split(""))!=-1){allow=false}if(key!=8&&key!=9&&key!=13&&key!=35&&key!=36&&key!=37&&key!=39&&key!=46){allow=false}else{if(typeof e.charCode!="undefined"){if(e.keyCode==e.which&&e.which!==0){allow=true;if(e.which==46){allow=false}}else if(e.keyCode!==0&&e.charCode===0&&e.which===0){allow=true}}}if(decimal&&key==decimal.charCodeAt(0)){if($.inArray(decimal,value.split(""))==-1){allow=true}else{allow=false}}}else{allow=true;if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,$(this).val().split(""));if(dot>=0&&$(this).val().length>dot+decimalPlaces){allow=false}}}return allow};$.fn.numeric.keyup=function(e){var val=$(this).val();if(val&&val.length>0){var carat=$.fn.getSelectionStart(this);var selectionEnd=$.fn.getSelectionEnd(this);var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");if(decimal!==""&&decimal!==null){var dot=$.inArray(decimal,val.split(""));if(dot===0){this.value="0"+val;carat++;selectionEnd++}if(dot==1&&val.charAt(0)=="-"){this.value="-0"+val.substring(1);carat++;selectionEnd++}val=this.value}var validChars=[0,1,2,3,4,5,6,7,8,9,"-",decimal];var length=val.length;for(var i=length-1;i>=0;i--){var ch=val.charAt(i);if(i!==0&&ch=="-"){val=val.substring(0,i)+val.substring(i+1)}else if(i===0&&!negative&&ch=="-"){val=val.substring(1)}var validChar=false;for(var j=0;j0){for(var k=length-1;k>firstDecimal;k--){var chch=val.charAt(k);if(chch==decimal){val=val.substring(0,k)+val.substring(k+1)}}}if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,val.split(""));if(dot>=0){val=val.substring(0,dot+decimalPlaces+1);selectionEnd=Math.min(val.length,selectionEnd)}}this.value=val;$.fn.setSelection(this,[carat,selectionEnd])}};$.fn.numeric.blur=function(){var decimal=$.data(this,"numeric.decimal");var callback=$.data(this,"numeric.callback");var negative=$.data(this,"numeric.negative");var val=this.value;if(val!==""){var re=new RegExp(negative?"-?":""+"^\\d+$|^\\d*"+decimal+"\\d+$");if(!re.exec(val)){callback.apply(this)}}};$.fn.removeNumeric=function(){return this.data("numeric.decimal",null).data("numeric.negative",null).data("numeric.callback",null).data("numeric.decimalPlaces",null).unbind("keypress",$.fn.numeric.keypress).unbind("keyup",$.fn.numeric.keyup).unbind("blur",$.fn.numeric.blur)};$.fn.getSelectionStart=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveEnd("character",o.value.length);if(r.text=="")return o.value.length;return Math.max(0,o.value.lastIndexOf(r.text))}else{try{return o.selectionStart}catch(e){return 0}}};$.fn.getSelectionEnd=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveStart("character",-o.value.length);return r.text.length}else return o.selectionEnd};$.fn.setSelection=function(o,p){if(typeof p=="number"){p=[p,p]}if(p&&p.constructor==Array&&p.length==2){if(o.type==="number"){o.focus()}else if(o.createTextRange){var r=o.createTextRange();r.collapse(true);r.moveStart("character",p[0]);r.moveEnd("character",p[1]-p[0]);r.select()}else{o.focus();try{if(o.setSelectionRange){o.setSelectionRange(p[0],p[1])}}catch(e){}}}}})(jQuery); From b711578781e810fecd927c70cb617aed2699222c Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 20 Mar 2015 19:05:29 +0100 Subject: [PATCH 06/12] Added AMD support --- numeric/jquery.numeric.js | 10 ++++++++-- numeric/jquery.numeric.min.js | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index f7b63e2..3410238 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -8,7 +8,13 @@ * Demo: http://www.texotela.co.uk/code/jquery/numeric/ * */ -(function($) { +(function(factory){ + if(typeof define === 'function' && define.amd){ + define(['jquery'], factory); + }else{ + factory(window.jQuery); + } +}(function($) { /* * Allows only valid characters to be entered into input boxes. * Note: fixes value when pasting via Ctrl+V, but not when using the mouse to paste @@ -336,4 +342,4 @@ $.fn.setSelection = function(o, p) } }; -})(jQuery); +})); diff --git a/numeric/jquery.numeric.min.js b/numeric/jquery.numeric.min.js index dd0bb58..425828f 100644 --- a/numeric/jquery.numeric.min.js +++ b/numeric/jquery.numeric.min.js @@ -8,4 +8,4 @@ * Demo: http://www.texotela.co.uk/code/jquery/numeric/ * */ -(function($){$.fn.numeric=function(config,callback){if(typeof config==="boolean"){config={decimal:config,negative:true,decimalPlaces:-1}}config=config||{};if(typeof config.negative=="undefined"){config.negative=true}var decimal=config.decimal===false?"":config.decimal||".";var negative=config.negative===true?true:false;var decimalPlaces=typeof config.decimalPlaces=="undefined"?-1:config.decimalPlaces;callback=typeof callback=="function"?callback:function(){};return this.data("numeric.decimal",decimal).data("numeric.negative",negative).data("numeric.callback",callback).data("numeric.decimalPlaces",decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur)};$.fn.numeric.keypress=function(e){var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");var key=e.charCode?e.charCode:e.keyCode?e.keyCode:0;if(key==13&&this.nodeName.toLowerCase()=="input"){return true}else if(key==13){return false}var allow=false;if(e.ctrlKey&&key==97||e.ctrlKey&&key==65){return true}if(e.ctrlKey&&key==120||e.ctrlKey&&key==88){return true}if(e.ctrlKey&&key==99||e.ctrlKey&&key==67){return true}if(e.ctrlKey&&key==122||e.ctrlKey&&key==90){return true}if(e.ctrlKey&&key==118||e.ctrlKey&&key==86||e.shiftKey&&key==45){return true}if(key<48||key>57){var value=$(this).val();if($.inArray("-",value.split(""))!==0&&negative&&key==45&&(value.length===0||parseInt($.fn.getSelectionStart(this),10)===0)){return true}if(decimal&&key==decimal.charCodeAt(0)&&$.inArray(decimal,value.split(""))!=-1){allow=false}if(key!=8&&key!=9&&key!=13&&key!=35&&key!=36&&key!=37&&key!=39&&key!=46){allow=false}else{if(typeof e.charCode!="undefined"){if(e.keyCode==e.which&&e.which!==0){allow=true;if(e.which==46){allow=false}}else if(e.keyCode!==0&&e.charCode===0&&e.which===0){allow=true}}}if(decimal&&key==decimal.charCodeAt(0)){if($.inArray(decimal,value.split(""))==-1){allow=true}else{allow=false}}}else{allow=true;if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,$(this).val().split(""));if(dot>=0&&$(this).val().length>dot+decimalPlaces){allow=false}}}return allow};$.fn.numeric.keyup=function(e){var val=$(this).val();if(val&&val.length>0){var carat=$.fn.getSelectionStart(this);var selectionEnd=$.fn.getSelectionEnd(this);var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");if(decimal!==""&&decimal!==null){var dot=$.inArray(decimal,val.split(""));if(dot===0){this.value="0"+val;carat++;selectionEnd++}if(dot==1&&val.charAt(0)=="-"){this.value="-0"+val.substring(1);carat++;selectionEnd++}val=this.value}var validChars=[0,1,2,3,4,5,6,7,8,9,"-",decimal];var length=val.length;for(var i=length-1;i>=0;i--){var ch=val.charAt(i);if(i!==0&&ch=="-"){val=val.substring(0,i)+val.substring(i+1)}else if(i===0&&!negative&&ch=="-"){val=val.substring(1)}var validChar=false;for(var j=0;j0){for(var k=length-1;k>firstDecimal;k--){var chch=val.charAt(k);if(chch==decimal){val=val.substring(0,k)+val.substring(k+1)}}}if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,val.split(""));if(dot>=0){val=val.substring(0,dot+decimalPlaces+1);selectionEnd=Math.min(val.length,selectionEnd)}}this.value=val;$.fn.setSelection(this,[carat,selectionEnd])}};$.fn.numeric.blur=function(){var decimal=$.data(this,"numeric.decimal");var callback=$.data(this,"numeric.callback");var negative=$.data(this,"numeric.negative");var val=this.value;if(val!==""){var re=new RegExp(negative?"-?":""+"^\\d+$|^\\d*"+decimal+"\\d+$");if(!re.exec(val)){callback.apply(this)}}};$.fn.removeNumeric=function(){return this.data("numeric.decimal",null).data("numeric.negative",null).data("numeric.callback",null).data("numeric.decimalPlaces",null).unbind("keypress",$.fn.numeric.keypress).unbind("keyup",$.fn.numeric.keyup).unbind("blur",$.fn.numeric.blur)};$.fn.getSelectionStart=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveEnd("character",o.value.length);if(r.text=="")return o.value.length;return Math.max(0,o.value.lastIndexOf(r.text))}else{try{return o.selectionStart}catch(e){return 0}}};$.fn.getSelectionEnd=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveStart("character",-o.value.length);return r.text.length}else return o.selectionEnd};$.fn.setSelection=function(o,p){if(typeof p=="number"){p=[p,p]}if(p&&p.constructor==Array&&p.length==2){if(o.type==="number"){o.focus()}else if(o.createTextRange){var r=o.createTextRange();r.collapse(true);r.moveStart("character",p[0]);r.moveEnd("character",p[1]-p[0]);r.select()}else{o.focus();try{if(o.setSelectionRange){o.setSelectionRange(p[0],p[1])}}catch(e){}}}}})(jQuery); +(function(factory){if(typeof define === 'function' && define.amd){define(['jquery'], factory);}else{factory(window.jQuery);}}(function($){$.fn.numeric=function(config,callback){if(typeof config==="boolean"){config={decimal:config,negative:true,decimalPlaces:-1}}config=config||{};if(typeof config.negative=="undefined"){config.negative=true}var decimal=config.decimal===false?"":config.decimal||".";var negative=config.negative===true?true:false;var decimalPlaces=typeof config.decimalPlaces=="undefined"?-1:config.decimalPlaces;callback=typeof callback=="function"?callback:function(){};return this.data("numeric.decimal",decimal).data("numeric.negative",negative).data("numeric.callback",callback).data("numeric.decimalPlaces",decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur)};$.fn.numeric.keypress=function(e){var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");var key=e.charCode?e.charCode:e.keyCode?e.keyCode:0;if(key==13&&this.nodeName.toLowerCase()=="input"){return true}else if(key==13){return false}var allow=false;if(e.ctrlKey&&key==97||e.ctrlKey&&key==65){return true}if(e.ctrlKey&&key==120||e.ctrlKey&&key==88){return true}if(e.ctrlKey&&key==99||e.ctrlKey&&key==67){return true}if(e.ctrlKey&&key==122||e.ctrlKey&&key==90){return true}if(e.ctrlKey&&key==118||e.ctrlKey&&key==86||e.shiftKey&&key==45){return true}if(key<48||key>57){var value=$(this).val();if($.inArray("-",value.split(""))!==0&&negative&&key==45&&(value.length===0||parseInt($.fn.getSelectionStart(this),10)===0)){return true}if(decimal&&key==decimal.charCodeAt(0)&&$.inArray(decimal,value.split(""))!=-1){allow=false}if(key!=8&&key!=9&&key!=13&&key!=35&&key!=36&&key!=37&&key!=39&&key!=46){allow=false}else{if(typeof e.charCode!="undefined"){if(e.keyCode==e.which&&e.which!==0){allow=true;if(e.which==46){allow=false}}else if(e.keyCode!==0&&e.charCode===0&&e.which===0){allow=true}}}if(decimal&&key==decimal.charCodeAt(0)){if($.inArray(decimal,value.split(""))==-1){allow=true}else{allow=false}}}else{allow=true;if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,$(this).val().split(""));if(dot>=0&&$(this).val().length>dot+decimalPlaces){allow=false}}}return allow};$.fn.numeric.keyup=function(e){var val=$(this).val();if(val&&val.length>0){var carat=$.fn.getSelectionStart(this);var selectionEnd=$.fn.getSelectionEnd(this);var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");if(decimal!==""&&decimal!==null){var dot=$.inArray(decimal,val.split(""));if(dot===0){this.value="0"+val;carat++;selectionEnd++}if(dot==1&&val.charAt(0)=="-"){this.value="-0"+val.substring(1);carat++;selectionEnd++}val=this.value}var validChars=[0,1,2,3,4,5,6,7,8,9,"-",decimal];var length=val.length;for(var i=length-1;i>=0;i--){var ch=val.charAt(i);if(i!==0&&ch=="-"){val=val.substring(0,i)+val.substring(i+1)}else if(i===0&&!negative&&ch=="-"){val=val.substring(1)}var validChar=false;for(var j=0;j0){for(var k=length-1;k>firstDecimal;k--){var chch=val.charAt(k);if(chch==decimal){val=val.substring(0,k)+val.substring(k+1)}}}if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,val.split(""));if(dot>=0){val=val.substring(0,dot+decimalPlaces+1);selectionEnd=Math.min(val.length,selectionEnd)}}this.value=val;$.fn.setSelection(this,[carat,selectionEnd])}};$.fn.numeric.blur=function(){var decimal=$.data(this,"numeric.decimal");var callback=$.data(this,"numeric.callback");var negative=$.data(this,"numeric.negative");var val=this.value;if(val!==""){var re=new RegExp(negative?"-?":""+"^\\d+$|^\\d*"+decimal+"\\d+$");if(!re.exec(val)){callback.apply(this)}}};$.fn.removeNumeric=function(){return this.data("numeric.decimal",null).data("numeric.negative",null).data("numeric.callback",null).data("numeric.decimalPlaces",null).unbind("keypress",$.fn.numeric.keypress).unbind("keyup",$.fn.numeric.keyup).unbind("blur",$.fn.numeric.blur)};$.fn.getSelectionStart=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveEnd("character",o.value.length);if(r.text=="")return o.value.length;return Math.max(0,o.value.lastIndexOf(r.text))}else{try{return o.selectionStart}catch(e){return 0}}};$.fn.getSelectionEnd=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveStart("character",-o.value.length);return r.text.length}else return o.selectionEnd};$.fn.setSelection=function(o,p){if(typeof p=="number"){p=[p,p]}if(p&&p.constructor==Array&&p.length==2){if(o.type==="number"){o.focus()}else if(o.createTextRange){var r=o.createTextRange();r.collapse(true);r.moveStart("character",p[0]);r.moveEnd("character",p[1]-p[0]);r.select()}else{o.focus();try{if(o.setSelectionRange){o.setSelectionRange(p[0],p[1])}}catch(e){}}}}})); From b0fe2510dcd3d5839cf6a7c5cc032a35586417b9 Mon Sep 17 00:00:00 2001 From: Borin Ouch Date: Thu, 9 Apr 2015 18:21:28 -0700 Subject: [PATCH 07/12] Fix input numeric input when decimal places full --- numeric/jquery.numeric.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index 3410238..e388548 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -146,8 +146,10 @@ $.fn.numeric.keypress = function(e) // remove extra decimal places if(decimal && decimalPlaces > 0) { + var selectionStart = $.fn.getSelectionStart(this); + var selectionEnd = $.fn.getSelectionEnd(this); var dot = $.inArray(decimal, $(this).val().split('')); - if (dot >= 0 && $(this).val().length > dot + decimalPlaces) { + if (selectionStart === selectionEnd && dot >= 0 && selectionStart > dot && $(this).val().length > dot + decimalPlaces) { allow = false; } } From 8a7ade600636acc7fb9d9b7251bd824871a9c850 Mon Sep 17 00:00:00 2001 From: Renato Moura Date: Tue, 12 May 2015 13:48:12 -0300 Subject: [PATCH 08/12] Update regexp validation --- numeric/jquery.numeric.js | 2 +- numeric/jquery.numeric.min.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index e388548..fc1a057 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -267,7 +267,7 @@ $.fn.numeric.blur = function() var val = this.value; if(val !== "") { - var re = new RegExp(negative?"-?":"" + "^\\d+$|^\\d*" + decimal + "\\d+$"); + var re = new RegExp("^" + (negative?"-?":"") + "\\d+$|^" + (negative?"-?":"") + "\\d*" + decimal + "\\d+$"); if(!re.exec(val)) { callback.apply(this); diff --git a/numeric/jquery.numeric.min.js b/numeric/jquery.numeric.min.js index 425828f..52f37a4 100644 --- a/numeric/jquery.numeric.min.js +++ b/numeric/jquery.numeric.min.js @@ -8,4 +8,4 @@ * Demo: http://www.texotela.co.uk/code/jquery/numeric/ * */ -(function(factory){if(typeof define === 'function' && define.amd){define(['jquery'], factory);}else{factory(window.jQuery);}}(function($){$.fn.numeric=function(config,callback){if(typeof config==="boolean"){config={decimal:config,negative:true,decimalPlaces:-1}}config=config||{};if(typeof config.negative=="undefined"){config.negative=true}var decimal=config.decimal===false?"":config.decimal||".";var negative=config.negative===true?true:false;var decimalPlaces=typeof config.decimalPlaces=="undefined"?-1:config.decimalPlaces;callback=typeof callback=="function"?callback:function(){};return this.data("numeric.decimal",decimal).data("numeric.negative",negative).data("numeric.callback",callback).data("numeric.decimalPlaces",decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur)};$.fn.numeric.keypress=function(e){var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");var key=e.charCode?e.charCode:e.keyCode?e.keyCode:0;if(key==13&&this.nodeName.toLowerCase()=="input"){return true}else if(key==13){return false}var allow=false;if(e.ctrlKey&&key==97||e.ctrlKey&&key==65){return true}if(e.ctrlKey&&key==120||e.ctrlKey&&key==88){return true}if(e.ctrlKey&&key==99||e.ctrlKey&&key==67){return true}if(e.ctrlKey&&key==122||e.ctrlKey&&key==90){return true}if(e.ctrlKey&&key==118||e.ctrlKey&&key==86||e.shiftKey&&key==45){return true}if(key<48||key>57){var value=$(this).val();if($.inArray("-",value.split(""))!==0&&negative&&key==45&&(value.length===0||parseInt($.fn.getSelectionStart(this),10)===0)){return true}if(decimal&&key==decimal.charCodeAt(0)&&$.inArray(decimal,value.split(""))!=-1){allow=false}if(key!=8&&key!=9&&key!=13&&key!=35&&key!=36&&key!=37&&key!=39&&key!=46){allow=false}else{if(typeof e.charCode!="undefined"){if(e.keyCode==e.which&&e.which!==0){allow=true;if(e.which==46){allow=false}}else if(e.keyCode!==0&&e.charCode===0&&e.which===0){allow=true}}}if(decimal&&key==decimal.charCodeAt(0)){if($.inArray(decimal,value.split(""))==-1){allow=true}else{allow=false}}}else{allow=true;if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,$(this).val().split(""));if(dot>=0&&$(this).val().length>dot+decimalPlaces){allow=false}}}return allow};$.fn.numeric.keyup=function(e){var val=$(this).val();if(val&&val.length>0){var carat=$.fn.getSelectionStart(this);var selectionEnd=$.fn.getSelectionEnd(this);var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");if(decimal!==""&&decimal!==null){var dot=$.inArray(decimal,val.split(""));if(dot===0){this.value="0"+val;carat++;selectionEnd++}if(dot==1&&val.charAt(0)=="-"){this.value="-0"+val.substring(1);carat++;selectionEnd++}val=this.value}var validChars=[0,1,2,3,4,5,6,7,8,9,"-",decimal];var length=val.length;for(var i=length-1;i>=0;i--){var ch=val.charAt(i);if(i!==0&&ch=="-"){val=val.substring(0,i)+val.substring(i+1)}else if(i===0&&!negative&&ch=="-"){val=val.substring(1)}var validChar=false;for(var j=0;j0){for(var k=length-1;k>firstDecimal;k--){var chch=val.charAt(k);if(chch==decimal){val=val.substring(0,k)+val.substring(k+1)}}}if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,val.split(""));if(dot>=0){val=val.substring(0,dot+decimalPlaces+1);selectionEnd=Math.min(val.length,selectionEnd)}}this.value=val;$.fn.setSelection(this,[carat,selectionEnd])}};$.fn.numeric.blur=function(){var decimal=$.data(this,"numeric.decimal");var callback=$.data(this,"numeric.callback");var negative=$.data(this,"numeric.negative");var val=this.value;if(val!==""){var re=new RegExp(negative?"-?":""+"^\\d+$|^\\d*"+decimal+"\\d+$");if(!re.exec(val)){callback.apply(this)}}};$.fn.removeNumeric=function(){return this.data("numeric.decimal",null).data("numeric.negative",null).data("numeric.callback",null).data("numeric.decimalPlaces",null).unbind("keypress",$.fn.numeric.keypress).unbind("keyup",$.fn.numeric.keyup).unbind("blur",$.fn.numeric.blur)};$.fn.getSelectionStart=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveEnd("character",o.value.length);if(r.text=="")return o.value.length;return Math.max(0,o.value.lastIndexOf(r.text))}else{try{return o.selectionStart}catch(e){return 0}}};$.fn.getSelectionEnd=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveStart("character",-o.value.length);return r.text.length}else return o.selectionEnd};$.fn.setSelection=function(o,p){if(typeof p=="number"){p=[p,p]}if(p&&p.constructor==Array&&p.length==2){if(o.type==="number"){o.focus()}else if(o.createTextRange){var r=o.createTextRange();r.collapse(true);r.moveStart("character",p[0]);r.moveEnd("character",p[1]-p[0]);r.select()}else{o.focus();try{if(o.setSelectionRange){o.setSelectionRange(p[0],p[1])}}catch(e){}}}}})); +(function(factory){if(typeof define === 'function' && define.amd){define(['jquery'], factory);}else{factory(window.jQuery);}}(function($){$.fn.numeric=function(config,callback){if(typeof config==="boolean"){config={decimal:config,negative:true,decimalPlaces:-1}}config=config||{};if(typeof config.negative=="undefined"){config.negative=true}var decimal=config.decimal===false?"":config.decimal||".";var negative=config.negative===true?true:false;var decimalPlaces=typeof config.decimalPlaces=="undefined"?-1:config.decimalPlaces;callback=typeof callback=="function"?callback:function(){};return this.data("numeric.decimal",decimal).data("numeric.negative",negative).data("numeric.callback",callback).data("numeric.decimalPlaces",decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur)};$.fn.numeric.keypress=function(e){var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");var key=e.charCode?e.charCode:e.keyCode?e.keyCode:0;if(key==13&&this.nodeName.toLowerCase()=="input"){return true}else if(key==13){return false}var allow=false;if(e.ctrlKey&&key==97||e.ctrlKey&&key==65){return true}if(e.ctrlKey&&key==120||e.ctrlKey&&key==88){return true}if(e.ctrlKey&&key==99||e.ctrlKey&&key==67){return true}if(e.ctrlKey&&key==122||e.ctrlKey&&key==90){return true}if(e.ctrlKey&&key==118||e.ctrlKey&&key==86||e.shiftKey&&key==45){return true}if(key<48||key>57){var value=$(this).val();if($.inArray("-",value.split(""))!==0&&negative&&key==45&&(value.length===0||parseInt($.fn.getSelectionStart(this),10)===0)){return true}if(decimal&&key==decimal.charCodeAt(0)&&$.inArray(decimal,value.split(""))!=-1){allow=false}if(key!=8&&key!=9&&key!=13&&key!=35&&key!=36&&key!=37&&key!=39&&key!=46){allow=false}else{if(typeof e.charCode!="undefined"){if(e.keyCode==e.which&&e.which!==0){allow=true;if(e.which==46){allow=false}}else if(e.keyCode!==0&&e.charCode===0&&e.which===0){allow=true}}}if(decimal&&key==decimal.charCodeAt(0)){if($.inArray(decimal,value.split(""))==-1){allow=true}else{allow=false}}}else{allow=true;if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,$(this).val().split(""));if(dot>=0&&$(this).val().length>dot+decimalPlaces){allow=false}}}return allow};$.fn.numeric.keyup=function(e){var val=$(this).val();if(val&&val.length>0){var carat=$.fn.getSelectionStart(this);var selectionEnd=$.fn.getSelectionEnd(this);var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");if(decimal!==""&&decimal!==null){var dot=$.inArray(decimal,val.split(""));if(dot===0){this.value="0"+val;carat++;selectionEnd++}if(dot==1&&val.charAt(0)=="-"){this.value="-0"+val.substring(1);carat++;selectionEnd++}val=this.value}var validChars=[0,1,2,3,4,5,6,7,8,9,"-",decimal];var length=val.length;for(var i=length-1;i>=0;i--){var ch=val.charAt(i);if(i!==0&&ch=="-"){val=val.substring(0,i)+val.substring(i+1)}else if(i===0&&!negative&&ch=="-"){val=val.substring(1)}var validChar=false;for(var j=0;j0){for(var k=length-1;k>firstDecimal;k--){var chch=val.charAt(k);if(chch==decimal){val=val.substring(0,k)+val.substring(k+1)}}}if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,val.split(""));if(dot>=0){val=val.substring(0,dot+decimalPlaces+1);selectionEnd=Math.min(val.length,selectionEnd)}}this.value=val;$.fn.setSelection(this,[carat,selectionEnd])}};$.fn.numeric.blur=function(){var decimal=$.data(this,"numeric.decimal");var callback=$.data(this,"numeric.callback");var negative=$.data(this,"numeric.negative");var val=this.value;if(val!==""){var re=new RegExp("^" + (negative?"-?":"") + "\\d+$|^" + (negative?"-?":"") + "\\d*" + decimal + "\\d+$");if(!re.exec(val)){callback.apply(this)}}};$.fn.removeNumeric=function(){return this.data("numeric.decimal",null).data("numeric.negative",null).data("numeric.callback",null).data("numeric.decimalPlaces",null).unbind("keypress",$.fn.numeric.keypress).unbind("keyup",$.fn.numeric.keyup).unbind("blur",$.fn.numeric.blur)};$.fn.getSelectionStart=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveEnd("character",o.value.length);if(r.text=="")return o.value.length;return Math.max(0,o.value.lastIndexOf(r.text))}else{try{return o.selectionStart}catch(e){return 0}}};$.fn.getSelectionEnd=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveStart("character",-o.value.length);return r.text.length}else return o.selectionEnd};$.fn.setSelection=function(o,p){if(typeof p=="number"){p=[p,p]}if(p&&p.constructor==Array&&p.length==2){if(o.type==="number"){o.focus()}else if(o.createTextRange){var r=o.createTextRange();r.collapse(true);r.moveStart("character",p[0]);r.moveEnd("character",p[1]-p[0]);r.select()}else{o.focus();try{if(o.setSelectionRange){o.setSelectionRange(p[0],p[1])}}catch(e){}}}}})); From 9450debf4c6ffccf21abe27d62fe3ee9382ea4fd Mon Sep 17 00:00:00 2001 From: Abiezer Matos Date: Thu, 28 May 2015 09:51:30 -0400 Subject: [PATCH 09/12] Dont allow keys using shift (#, $, %) --- numeric/jquery.numeric.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index e388548..9da87c0 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -70,6 +70,10 @@ $.fn.numeric.keypress = function(e) { return false; } + //dont allow #, $, % + else if(key == 35 || key == 36 || key == 37){ + return false; + } var allow = false; // allow Ctrl+A if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) { return true; } From c9ebb658adb258212a8b561dfc38a0f768e0263a Mon Sep 17 00:00:00 2001 From: MaRoed Date: Thu, 10 Dec 2015 08:23:13 +0100 Subject: [PATCH 10/12] upgrade altDecimal Define an alternative key as decimal separator. example: Press the key for the standard decimal separator (.) or the alternative separator (,). In both cases the standard separator (.) is inserted into the input field. This can be useful for keyboards with comma on the number pad. --- numeric/jquery.numeric.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index 6f31f69..2342d4e 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -27,6 +27,7 @@ * @example $(".numeric").numeric(); * @example $(".numeric").numeric(","); // use , as separator * @example $(".numeric").numeric({ decimal : "," }); // use , as separator + * @example $(".numeric").numeric({ altDecimal : "," }); // accept , as alternative separator, but use . as separator in output * @example $(".numeric").numeric({ negative : false }); // do not allow negative values * @example $(".numeric").numeric({ decimalPlaces : 2 }); // only allow 2 decimal places * @example $(".numeric").numeric(null, callback); // use default values, pass on the 'callback' function @@ -43,14 +44,16 @@ $.fn.numeric = function(config, callback) if(typeof config.negative == "undefined") { config.negative = true; } // set decimal point var decimal = (config.decimal === false) ? "" : config.decimal || "."; + // set alternative key as decimal point + var altDecimal = (config.altDecimal === false) ? "" : config.altDecimal || decimal; // allow negatives var negative = (config.negative === true) ? true : false; - // set decimal places + // set decimal places var decimalPlaces = (typeof config.decimalPlaces == "undefined") ? -1 : config.decimalPlaces; // callback function callback = (typeof(callback) == "function" ? callback : function() {}); // set data and methods - return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.callback", callback).data("numeric.decimalPlaces", decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur); + return this.data("numeric.decimal", decimal).data("numeric.altDecimal", altDecimal).data("numeric.negative", negative).data("numeric.callback", callback).data("numeric.decimalPlaces", decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur); }; $.fn.numeric.keypress = function(e) @@ -58,7 +61,9 @@ $.fn.numeric.keypress = function(e) // get decimal character and determine if negatives are allowed var decimal = $.data(this, "numeric.decimal"); var negative = $.data(this, "numeric.negative"); - var decimalPlaces = $.data(this, "numeric.decimalPlaces"); + var decimalPlaces = $.data(this, "numeric.decimalPlaces"); + // get an alternative decimal separator + var altDecimal = $.data(this, "numeric.altDecimal"); // get the key that was pressed var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0; // allow enter/return key (only when in an input box) @@ -131,8 +136,8 @@ $.fn.numeric.keypress = function(e) } } } - // if key pressed is the decimal and it is not already in the field - if(decimal && key == decimal.charCodeAt(0)) + // if key pressed is the decimal or altDecimal and decimal is not already in the field + if(decimal && key == decimal.charCodeAt(0) || altDecimal && key == altDecimal.charCodeAt(0)) { if($.inArray(decimal, value.split('')) == -1) { @@ -173,7 +178,9 @@ $.fn.numeric.keyup = function(e) // get decimal character and determine if negatives are allowed var decimal = $.data(this, "numeric.decimal"); var negative = $.data(this, "numeric.negative"); - var decimalPlaces = $.data(this, "numeric.decimalPlaces"); + var decimalPlaces = $.data(this, "numeric.decimalPlaces"); + // get an alternative decimal separator + var altDecimal = $.data(this, "numeric.altDecimal"); // prepend a 0 if necessary if(decimal !== "" && decimal !== null) @@ -226,6 +233,12 @@ $.fn.numeric.keyup = function(e) break; } } + // if not a valid character and character is altDecimal, replace + if(!validChar && ch == altDecimal) + { + val = val.substring(0, i) + decimal + val.substring(i + 1); + validChar = true; + } // if not a valid character, or a space, remove if(!validChar || ch == " ") { @@ -281,7 +294,7 @@ $.fn.numeric.blur = function() $.fn.removeNumeric = function() { - return this.data("numeric.decimal", null).data("numeric.negative", null).data("numeric.callback", null).data("numeric.decimalPlaces", null).unbind("keypress", $.fn.numeric.keypress).unbind("keyup", $.fn.numeric.keyup).unbind("blur", $.fn.numeric.blur); + return this.data("numeric.decimal", null).data("numeric.altDecimal", null).data("numeric.negative", null).data("numeric.callback", null).data("numeric.decimalPlaces", null).unbind("keypress", $.fn.numeric.keypress).unbind("keyup", $.fn.numeric.keyup).unbind("blur", $.fn.numeric.blur); }; // Based on code from http://javascript.nwbox.com/cursor_position/ (Diego Perini ) From a9417702a727296d84035a43610a039e1ea95cec Mon Sep 17 00:00:00 2001 From: MaRoed Date: Thu, 10 Dec 2015 08:42:22 +0100 Subject: [PATCH 11/12] upgrade alternative decimal separator Define an alternative key as decimal separator. example: Press the key for the standard decimal separator (.) or the alternative separator (,). In both cases the standard separator (.) is inserted into the input field. This can be useful for keyboards with comma on the number pad. --- numeric/jquery.numeric.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numeric/jquery.numeric.min.js b/numeric/jquery.numeric.min.js index 52f37a4..f21817c 100644 --- a/numeric/jquery.numeric.min.js +++ b/numeric/jquery.numeric.min.js @@ -8,4 +8,4 @@ * Demo: http://www.texotela.co.uk/code/jquery/numeric/ * */ -(function(factory){if(typeof define === 'function' && define.amd){define(['jquery'], factory);}else{factory(window.jQuery);}}(function($){$.fn.numeric=function(config,callback){if(typeof config==="boolean"){config={decimal:config,negative:true,decimalPlaces:-1}}config=config||{};if(typeof config.negative=="undefined"){config.negative=true}var decimal=config.decimal===false?"":config.decimal||".";var negative=config.negative===true?true:false;var decimalPlaces=typeof config.decimalPlaces=="undefined"?-1:config.decimalPlaces;callback=typeof callback=="function"?callback:function(){};return this.data("numeric.decimal",decimal).data("numeric.negative",negative).data("numeric.callback",callback).data("numeric.decimalPlaces",decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur)};$.fn.numeric.keypress=function(e){var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");var key=e.charCode?e.charCode:e.keyCode?e.keyCode:0;if(key==13&&this.nodeName.toLowerCase()=="input"){return true}else if(key==13){return false}var allow=false;if(e.ctrlKey&&key==97||e.ctrlKey&&key==65){return true}if(e.ctrlKey&&key==120||e.ctrlKey&&key==88){return true}if(e.ctrlKey&&key==99||e.ctrlKey&&key==67){return true}if(e.ctrlKey&&key==122||e.ctrlKey&&key==90){return true}if(e.ctrlKey&&key==118||e.ctrlKey&&key==86||e.shiftKey&&key==45){return true}if(key<48||key>57){var value=$(this).val();if($.inArray("-",value.split(""))!==0&&negative&&key==45&&(value.length===0||parseInt($.fn.getSelectionStart(this),10)===0)){return true}if(decimal&&key==decimal.charCodeAt(0)&&$.inArray(decimal,value.split(""))!=-1){allow=false}if(key!=8&&key!=9&&key!=13&&key!=35&&key!=36&&key!=37&&key!=39&&key!=46){allow=false}else{if(typeof e.charCode!="undefined"){if(e.keyCode==e.which&&e.which!==0){allow=true;if(e.which==46){allow=false}}else if(e.keyCode!==0&&e.charCode===0&&e.which===0){allow=true}}}if(decimal&&key==decimal.charCodeAt(0)){if($.inArray(decimal,value.split(""))==-1){allow=true}else{allow=false}}}else{allow=true;if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,$(this).val().split(""));if(dot>=0&&$(this).val().length>dot+decimalPlaces){allow=false}}}return allow};$.fn.numeric.keyup=function(e){var val=$(this).val();if(val&&val.length>0){var carat=$.fn.getSelectionStart(this);var selectionEnd=$.fn.getSelectionEnd(this);var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");if(decimal!==""&&decimal!==null){var dot=$.inArray(decimal,val.split(""));if(dot===0){this.value="0"+val;carat++;selectionEnd++}if(dot==1&&val.charAt(0)=="-"){this.value="-0"+val.substring(1);carat++;selectionEnd++}val=this.value}var validChars=[0,1,2,3,4,5,6,7,8,9,"-",decimal];var length=val.length;for(var i=length-1;i>=0;i--){var ch=val.charAt(i);if(i!==0&&ch=="-"){val=val.substring(0,i)+val.substring(i+1)}else if(i===0&&!negative&&ch=="-"){val=val.substring(1)}var validChar=false;for(var j=0;j0){for(var k=length-1;k>firstDecimal;k--){var chch=val.charAt(k);if(chch==decimal){val=val.substring(0,k)+val.substring(k+1)}}}if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,val.split(""));if(dot>=0){val=val.substring(0,dot+decimalPlaces+1);selectionEnd=Math.min(val.length,selectionEnd)}}this.value=val;$.fn.setSelection(this,[carat,selectionEnd])}};$.fn.numeric.blur=function(){var decimal=$.data(this,"numeric.decimal");var callback=$.data(this,"numeric.callback");var negative=$.data(this,"numeric.negative");var val=this.value;if(val!==""){var re=new RegExp("^" + (negative?"-?":"") + "\\d+$|^" + (negative?"-?":"") + "\\d*" + decimal + "\\d+$");if(!re.exec(val)){callback.apply(this)}}};$.fn.removeNumeric=function(){return this.data("numeric.decimal",null).data("numeric.negative",null).data("numeric.callback",null).data("numeric.decimalPlaces",null).unbind("keypress",$.fn.numeric.keypress).unbind("keyup",$.fn.numeric.keyup).unbind("blur",$.fn.numeric.blur)};$.fn.getSelectionStart=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveEnd("character",o.value.length);if(r.text=="")return o.value.length;return Math.max(0,o.value.lastIndexOf(r.text))}else{try{return o.selectionStart}catch(e){return 0}}};$.fn.getSelectionEnd=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveStart("character",-o.value.length);return r.text.length}else return o.selectionEnd};$.fn.setSelection=function(o,p){if(typeof p=="number"){p=[p,p]}if(p&&p.constructor==Array&&p.length==2){if(o.type==="number"){o.focus()}else if(o.createTextRange){var r=o.createTextRange();r.collapse(true);r.moveStart("character",p[0]);r.moveEnd("character",p[1]-p[0]);r.select()}else{o.focus();try{if(o.setSelectionRange){o.setSelectionRange(p[0],p[1])}}catch(e){}}}}})); +(function(factory){if(typeof define === 'function' && define.amd){define(['jquery'], factory);}else{factory(window.jQuery);}}(function($){$.fn.numeric=function(config,callback){if(typeof config==="boolean"){config={decimal:config,negative:true,decimalPlaces:-1}}config=config||{};if(typeof config.negative=="undefined"){config.negative=true}var decimal=config.decimal===false?"":config.decimal||".";var altDecimal=config.altDecimal===false?"": config.altDecimal||decimal;var negative=config.negative===true?true:false;var decimalPlaces=typeof config.decimalPlaces=="undefined"?-1:config.decimalPlaces;callback=typeof callback=="function"?callback:function(){};return this.data("numeric.decimal",decimal).data("numeric.altDecimal",altDecimal).data("numeric.negative",negative).data("numeric.callback",callback).data("numeric.decimalPlaces",decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur)};$.fn.numeric.keypress=function(e){var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");var altDecimal=$.data(this,"numeric.altDecimal");var key=e.charCode?e.charCode:e.keyCode?e.keyCode:0;if(key==13&&this.nodeName.toLowerCase()=="input"){return true}else if(key==13){return false}var allow=false;if(e.ctrlKey&&key==97||e.ctrlKey&&key==65){return true}if(e.ctrlKey&&key==120||e.ctrlKey&&key==88){return true}if(e.ctrlKey&&key==99||e.ctrlKey&&key==67){return true}if(e.ctrlKey&&key==122||e.ctrlKey&&key==90){return true}if(e.ctrlKey&&key==118||e.ctrlKey&&key==86||e.shiftKey&&key==45){return true}if(key<48||key>57){var value=$(this).val();if($.inArray("-",value.split(""))!==0&&negative&&key==45&&(value.length===0||parseInt($.fn.getSelectionStart(this),10)===0)){return true}if(decimal&&key==decimal.charCodeAt(0)&&$.inArray(decimal,value.split(""))!=-1){allow=false}if(key!=8&&key!=9&&key!=13&&key!=35&&key!=36&&key!=37&&key!=39&&key!=46){allow=false}else{if(typeof e.charCode!="undefined"){if(e.keyCode==e.which&&e.which!==0){allow=true;if(e.which==46){allow=false}}else if(e.keyCode!==0&&e.charCode===0&&e.which===0){allow=true}}}if(decimal&&key==decimal.charCodeAt(0)||altDecimal&&key==altDecimal.charCodeAt(0)){if($.inArray(decimal,value.split(""))==-1){allow=true}else{allow=false}}}else{allow=true;if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,$(this).val().split(""));if(dot>=0&&$(this).val().length>dot+decimalPlaces){allow=false}}}return allow};$.fn.numeric.keyup=function(e){var val=$(this).val();if(val&&val.length>0){var carat=$.fn.getSelectionStart(this);var selectionEnd=$.fn.getSelectionEnd(this);var decimal=$.data(this,"numeric.decimal");var negative=$.data(this,"numeric.negative");var decimalPlaces=$.data(this,"numeric.decimalPlaces");var altDecimal=$.data(this,"numeric.altDecimal");if(decimal!==""&&decimal!==null){var dot=$.inArray(decimal,val.split(""));if(dot===0){this.value="0"+val;carat++;selectionEnd++}if(dot==1&&val.charAt(0)=="-"){this.value="-0"+val.substring(1);carat++;selectionEnd++}val=this.value}var validChars=[0,1,2,3,4,5,6,7,8,9,"-",decimal];var length=val.length;for(var i=length-1;i>=0;i--){var ch=val.charAt(i);if(i!==0&&ch=="-"){val=val.substring(0,i)+val.substring(i+1)}else if(i===0&&!negative&&ch=="-"){val=val.substring(1)}var validChar=false;for(var j=0;j0){for(var k=length-1;k>firstDecimal;k--){var chch=val.charAt(k);if(chch==decimal){val=val.substring(0,k)+val.substring(k+1)}}}if(decimal&&decimalPlaces>0){var dot=$.inArray(decimal,val.split(""));if(dot>=0){val=val.substring(0,dot+decimalPlaces+1);selectionEnd=Math.min(val.length,selectionEnd)}}this.value=val;$.fn.setSelection(this,[carat,selectionEnd])}};$.fn.numeric.blur=function(){var decimal=$.data(this,"numeric.decimal");var callback=$.data(this,"numeric.callback");var negative=$.data(this,"numeric.negative");var val=this.value;if(val!==""){var re=new RegExp("^" + (negative?"-?":"") + "\\d+$|^" + (negative?"-?":"") + "\\d*" + decimal + "\\d+$");if(!re.exec(val)){callback.apply(this)}}};$.fn.removeNumeric=function(){return this.data("numeric.decimal",null).data("numeric.altDecimal",null).data("numeric.negative",null).data("numeric.callback",null).data("numeric.decimalPlaces",null).unbind("keypress",$.fn.numeric.keypress).unbind("keyup",$.fn.numeric.keyup).unbind("blur",$.fn.numeric.blur)};$.fn.getSelectionStart=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveEnd("character",o.value.length);if(r.text=="")return o.value.length;return Math.max(0,o.value.lastIndexOf(r.text))}else{try{return o.selectionStart}catch(e){return 0}}};$.fn.getSelectionEnd=function(o){if(o.type==="number"){return undefined}else if(o.createTextRange&&document.selection){var r=document.selection.createRange().duplicate();r.moveStart("character",-o.value.length);return r.text.length}else return o.selectionEnd};$.fn.setSelection=function(o,p){if(typeof p=="number"){p=[p,p]}if(p&&p.constructor==Array&&p.length==2){if(o.type==="number"){o.focus()}else if(o.createTextRange){var r=o.createTextRange();r.collapse(true);r.moveStart("character",p[0]);r.moveEnd("character",p[1]-p[0]);r.select()}else{o.focus();try{if(o.setSelectionRange){o.setSelectionRange(p[0],p[1])}}catch(e){}}}}})); From 8961c1da5e2f2830cf11e1b368fe4c1f9f6f94d1 Mon Sep 17 00:00:00 2001 From: MaRoed Date: Thu, 10 Dec 2015 08:48:51 +0100 Subject: [PATCH 12/12] upgrade examples alternative decimal separators Added examples for altDecimal and reverse use of altDecimal. --- numeric/test.html | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/numeric/test.html b/numeric/test.html index 54fe2df..62f91be 100644 --- a/numeric/test.html +++ b/numeric/test.html @@ -20,6 +20,12 @@

Numbers with up to 2 decimal places: +

+ Alternative (,) changes to standard (.) decimal separator: + +

+ Reverse change from alternative (.) to standard (,) decimal separator: +

Remove numeric @@ -28,12 +34,14 @@ $(".integer").numeric(false, function() { alert("Integers only"); this.value = ""; this.focus(); }); $(".positive").numeric({ negative: false }, function() { alert("No negative values"); this.value = ""; this.focus(); }); $(".positive-integer").numeric({ decimal: false, negative: false }, function() { alert("Positive integers only"); this.value = ""; this.focus(); }); - $(".decimal-2-places").numeric({ decimalPlaces: 2 }); + $(".decimal-2-places").numeric({ decimalPlaces: 2 }); + $(".alternative-decimal-separator").numeric({ altDecimal: "," }); + $(".alternative-decimal-separator-reverse").numeric({ altDecimal: ".", decimal : "," }); $("#remove").click( function(e) { e.preventDefault(); - $(".numeric,.integer,.positive,.positive-integer,.decimal-2-places").removeNumeric(); + $(".numeric,.integer,.positive,.positive-integer,.decimal-2-places,.alternative-decimal-separator,.alternative-decimal-separator-reverse").removeNumeric(); } );