diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index f7b63e2..0fb4a44 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -11,7 +11,7 @@ (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 + * Note: fixes value when pasting via Ctrl|Cmd+V, but not when using the mouse to paste * side-effect: Ctrl+A does not work, though you can still use the mouse to select (or double-click to select all) * * @name numeric @@ -65,16 +65,16 @@ $.fn.numeric.keypress = function(e) return false; } var allow = false; - // allow Ctrl+A - if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) { return true; } - // allow Ctrl+X (cut) - if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) { return true; } - // allow Ctrl+C (copy) - if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) { return true; } - // allow Ctrl+Z (undo) - if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) { return true; } - // allow or deny Ctrl+V (paste), Shift+Ins - if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */ || + // allow Ctrl+A or (Mac)Cmd+A + if(((e.ctrlKey||e.metaKey) && key == 97 /* firefox */) || ((e.ctrlKey||e.metaKey) && key == 65) /* opera */) { return true; } + // allow Ctrl+X or (Mac)Cmd+X (cut) + if(((e.ctrlKey||e.metaKey) && key == 120 /* firefox */) || ((e.ctrlKey||e.metaKey) && key == 88) /* opera */) { return true; } + // allow Ctrl+C or (Mac)Cmd+C (copy) + if(((e.ctrlKey||e.metaKey) && key == 99 /* firefox */) || ((e.ctrlKey||e.metaKey) && key == 67) /* opera */) { return true; } + // allow Ctrl+Z or (Mac)Cmd+Z (undo) + if(((e.ctrlKey||e.metaKey) && key == 122 /* firefox */) || ((e.ctrlKey||e.metaKey) && key == 90) /* opera */) { return true; } + // allow or deny Ctrl+V or (Mac)Cmd+V (paste), Shift+Ins + if(((e.ctrlKey||e.metaKey) && key == 118 /* firefox */) || ((e.ctrlKey||e.metaKey) && key == 86) /* opera */ || (e.shiftKey && key == 45)) { return true; } // if a number was not pressed if(key < 48 || key > 57) diff --git a/numeric/jquery.numeric.min.js b/numeric/jquery.numeric.min.js index dd0bb58..346052a 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(e){e.fn.numeric=function(t,n){if(typeof t==="boolean"){t={decimal:t,negative:true,decimalPlaces:-1}}t=t||{};if(typeof t.negative=="undefined"){t.negative=true}var r=t.decimal===false?"":t.decimal||".";var i=t.negative===true?true:false;var s=typeof t.decimalPlaces=="undefined"?-1:t.decimalPlaces;n=typeof n=="function"?n:function(){};return this.data("numeric.decimal",r).data("numeric.negative",i).data("numeric.callback",n).data("numeric.decimalPlaces",s).keypress(e.fn.numeric.keypress).keyup(e.fn.numeric.keyup).blur(e.fn.numeric.blur)};e.fn.numeric.keypress=function(t){var n=e.data(this,"numeric.decimal");var r=e.data(this,"numeric.negative");var i=e.data(this,"numeric.decimalPlaces");var s=t.charCode?t.charCode:t.keyCode?t.keyCode:0;if(s==13&&this.nodeName.toLowerCase()=="input"){return true}else if(s==13){return false}var o=false;if((t.ctrlKey||t.metaKey)&&s==97||(t.ctrlKey||t.metaKey)&&s==65){return true}if((t.ctrlKey||t.metaKey)&&s==120||(t.ctrlKey||t.metaKey)&&s==88){return true}if((t.ctrlKey||t.metaKey)&&s==99||(t.ctrlKey||t.metaKey)&&s==67){return true}if((t.ctrlKey||t.metaKey)&&s==122||(t.ctrlKey||t.metaKey)&&s==90){return true}if((t.ctrlKey||t.metaKey)&&s==118||(t.ctrlKey||t.metaKey)&&s==86||t.shiftKey&&s==45){return true}if(s<48||s>57){var u=e(this).val();if(e.inArray("-",u.split(""))!==0&&r&&s==45&&(u.length===0||parseInt(e.fn.getSelectionStart(this),10)===0)){return true}if(n&&s==n.charCodeAt(0)&&e.inArray(n,u.split(""))!=-1){o=false}if(s!=8&&s!=9&&s!=13&&s!=35&&s!=36&&s!=37&&s!=39&&s!=46){o=false}else{if(typeof t.charCode!="undefined"){if(t.keyCode==t.which&&t.which!==0){o=true;if(t.which==46){o=false}}else if(t.keyCode!==0&&t.charCode===0&&t.which===0){o=true}}}if(n&&s==n.charCodeAt(0)){if(e.inArray(n,u.split(""))==-1){o=true}else{o=false}}}else{o=true;if(n&&i>0){var a=e.inArray(n,e(this).val().split(""));if(a>=0&&e(this).val().length>a+i){o=false}}}return o};e.fn.numeric.keyup=function(t){var n=e(this).val();if(n&&n.length>0){var r=e.fn.getSelectionStart(this);var i=e.fn.getSelectionEnd(this);var s=e.data(this,"numeric.decimal");var o=e.data(this,"numeric.negative");var u=e.data(this,"numeric.decimalPlaces");if(s!==""&&s!==null){var a=e.inArray(s,n.split(""));if(a===0){this.value="0"+n;r++;i++}if(a==1&&n.charAt(0)=="-"){this.value="-0"+n.substring(1);r++;i++}n=this.value}var f=[0,1,2,3,4,5,6,7,8,9,"-",s];var l=n.length;for(var c=l-1;c>=0;c--){var h=n.charAt(c);if(c!==0&&h=="-"){n=n.substring(0,c)+n.substring(c+1)}else if(c===0&&!o&&h=="-"){n=n.substring(1)}var p=false;for(var d=0;d0){for(var m=l-1;m>v;m--){var g=n.charAt(m);if(g==s){n=n.substring(0,m)+n.substring(m+1)}}}if(s&&u>0){var a=e.inArray(s,n.split(""));if(a>=0){n=n.substring(0,a+u+1);i=Math.min(n.length,i)}}this.value=n;e.fn.setSelection(this,[r,i])}};e.fn.numeric.blur=function(){var t=e.data(this,"numeric.decimal");var n=e.data(this,"numeric.callback");var r=e.data(this,"numeric.negative");var i=this.value;if(i!==""){var s=new RegExp(r?"-?":""+"^\\d+$|^\\d*"+t+"\\d+$");if(!s.exec(i)){n.apply(this)}}};e.fn.removeNumeric=function(){return this.data("numeric.decimal",null).data("numeric.negative",null).data("numeric.callback",null).data("numeric.decimalPlaces",null).unbind("keypress",e.fn.numeric.keypress).unbind("keyup",e.fn.numeric.keyup).unbind("blur",e.fn.numeric.blur)};e.fn.getSelectionStart=function(e){if(e.type==="number"){return undefined}else if(e.createTextRange&&document.selection){var t=document.selection.createRange().duplicate();t.moveEnd("character",e.value.length);if(t.text=="")return e.value.length;return Math.max(0,e.value.lastIndexOf(t.text))}else{try{return e.selectionStart}catch(n){return 0}}};e.fn.getSelectionEnd=function(e){if(e.type==="number"){return undefined}else if(e.createTextRange&&document.selection){var t=document.selection.createRange().duplicate();t.moveStart("character",-e.value.length);return t.text.length}else return e.selectionEnd};e.fn.setSelection=function(e,t){if(typeof t=="number"){t=[t,t]}if(t&&t.constructor==Array&&t.length==2){if(e.type==="number"){e.focus()}else if(e.createTextRange){var n=e.createTextRange();n.collapse(true);n.moveStart("character",t[0]);n.moveEnd("character",t[1]-t[0]);n.select()}else{e.focus();try{if(e.setSelectionRange){e.setSelectionRange(t[0],t[1])}}catch(r){}}}}})(jQuery) \ No newline at end of file