diff --git a/jquery.number.js b/jquery.number.js index 438acd5..b713d0e 100644 --- a/jquery.number.js +++ b/jquery.number.js @@ -703,8 +703,8 @@ // { // num = '-'+num; // } - // Otherwise, don't worry about other valhooks, just run ours. - return el.value = num; + + return origHookSet(el, num); } }; diff --git a/jquery.number.min.js b/jquery.number.min.js index 4fce02b..c8a2942 100644 --- a/jquery.number.min.js +++ b/jquery.number.min.js @@ -1,2 +1,2 @@ -/*! jQuery number 2.1.5 (c) github.com/teamdf/jquery-number | opensource.teamdf.com/license */ -(function(e){"use strict";function t(e,t){if(this.createTextRange){var n=this.createTextRange();n.collapse(true);n.moveStart("character",e);n.moveEnd("character",t-e);n.select()}else if(this.setSelectionRange){this.focus();this.setSelectionRange(e,t)}}function n(e){var t=this.value.length;e=e.toLowerCase()=="start"?"Start":"End";if(document.selection){var n=document.selection.createRange(),r,i,s;r=n.duplicate();r.expand("textedit");r.setEndPoint("EndToEnd",n);i=r.text.length-n.text.length;s=i+n.text.length;return e=="Start"?i:s}else if(typeof this["selection"+e]!="undefined"){t=this["selection"+e]}return t}var r={codes:{46:127,188:44,109:45,190:46,191:47,192:96,220:92,222:39,221:93,219:91,173:45,187:61,186:59,189:45,110:46},shifts:{96:"~",49:"!",50:"@",51:"#",52:"$",53:"%",54:"^",55:"&",56:"*",57:"(",48:")",45:"_",61:"+",91:"{",93:"}",92:"|",59:":",39:'"',44:"<",46:">",47:"?"}};e.fn.number=function(i,s,o,u){u=typeof u==="undefined"?",":u;o=typeof o==="undefined"?".":o;s=typeof s==="undefined"?0:s;var a="\\u"+("0000"+o.charCodeAt(0).toString(16)).slice(-4),f=new RegExp("[^"+a+"0-9]","g"),l=new RegExp(a,"g");if(i===true){if(this.is("input:text")){return this.on({"keydown.format":function(i){var a=e(this),f=a.data("numFormat"),l=i.keyCode?i.keyCode:i.which,c="",h=n.apply(this,["start"]),p=n.apply(this,["end"]),d="",v=false;if(r.codes.hasOwnProperty(l)){l=r.codes[l]}if(!i.shiftKey&&l>=65&&l<=90){l+=32}else if(!i.shiftKey&&l>=69&&l<=105){l-=48}else if(i.shiftKey&&r.shifts.hasOwnProperty(l)){c=r.shifts[l]}if(c=="")c=String.fromCharCode(l);if(l!=8&&l!=45&&l!=127&&c!=o&&!c.match(/[0-9]/)){var m=i.keyCode?i.keyCode:i.which;if(m==46||m==8||m==127||m==9||m==27||m==13||(m==65||m==82||m==80||m==83||m==70||m==72||m==66||m==74||m==84||m==90||m==61||m==173||m==48)&&(i.ctrlKey||i.metaKey)===true||(m==86||m==67||m==88)&&(i.ctrlKey||i.metaKey)===true||m>=35&&m<=39||m>=112&&m<=123){return}i.preventDefault();return false}if(h==0&&p==this.value.length||a.val()==0){if(l==8){h=p=1;this.value="";f.init=s>0?-1:0;f.c=s>0?-(s+1):0;t.apply(this,[0,0])}else if(c==o){h=p=1;this.value="0"+o+(new Array(s+1)).join("0");f.init=s>0?1:0;f.c=s>0?-(s+1):0}else if(l==45){h=p=2;this.value="-0"+o+(new Array(s+1)).join("0");f.init=s>0?1:0;f.c=s>0?-(s+1):0;t.apply(this,[2,2])}else{f.init=s>0?-1:0;f.c=s>0?-s:0}}else{f.c=p-this.value.length}f.isPartialSelection=h==p?false:true;if(s>0&&c==o&&h==this.value.length-s-1){f.c++;f.init=Math.max(0,f.init);i.preventDefault();v=this.value.length+f.c}else if(l==45&&(h!=0||this.value.indexOf("-")==0)){i.preventDefault()}else if(c==o){f.init=Math.max(0,f.init);i.preventDefault()}else if(s>0&&l==127&&h==this.value.length-s-1){i.preventDefault()}else if(s>0&&l==8&&h==this.value.length-s){i.preventDefault();f.c--;v=this.value.length+f.c}else if(s>0&&l==127&&h>this.value.length-s-1){if(this.value==="")return;if(this.value.slice(h,h+1)!="0"){d=this.value.slice(0,h)+"0"+this.value.slice(h+1);a.val(d)}i.preventDefault();v=this.value.length+f.c}else if(s>0&&l==8&&h>this.value.length-s){if(this.value==="")return;if(this.value.slice(h-1,h)!="0"){d=this.value.slice(0,h-1)+"0"+this.value.slice(h);a.val(d)}i.preventDefault();f.c--;v=this.value.length+f.c}else if(l==127&&this.value.slice(h,h+1)==u){i.preventDefault()}else if(l==8&&this.value.slice(h-1,h)==u){i.preventDefault();f.c--;v=this.value.length+f.c}else if(s>0&&h==p&&this.value.length>s+1&&h>this.value.length-s-1&&isFinite(+c)&&!i.metaKey&&!i.ctrlKey&&!i.altKey&&c.length===1){if(p===this.value.length){d=this.value.slice(0,h-1)}else{d=this.value.slice(0,h)+this.value.slice(h+1)}this.value=d;v=h}if(v!==false){t.apply(this,[v,v])}a.data("numFormat",f)},"keyup.format":function(r){var i=e(this),o=i.data("numFormat"),u=r.keyCode?r.keyCode:r.which,a=n.apply(this,["start"]),f=n.apply(this,["end"]),l;if(a===0&&f===0&&(u===189||u===109)){i.val("-"+i.val());a=1;o.c=1-this.value.length;o.init=1;i.data("numFormat",o);l=this.value.length+o.c;t.apply(this,[l,l])}if(this.value===""||(u<48||u>57)&&(u<96||u>105)&&u!==8&&u!==46&&u!==110)return;i.val(i.val());if(s>0){if(o.init<1){a=this.value.length-s-(o.init<0?1:0);o.c=a-this.value.length;o.init=1;i.data("numFormat",o)}else if(a>this.value.length-s&&u!=8){o.c++;i.data("numFormat",o)}}if(u==46&&!o.isPartialSelection){o.c++;i.data("numFormat",o)}l=this.value.length+o.c;t.apply(this,[l,l])},"paste.format":function(t){var n=e(this),r=t.originalEvent,i=null;if(window.clipboardData&&window.clipboardData.getData){i=window.clipboardData.getData("Text")}else if(r.clipboardData&&r.clipboardData.getData){i=r.clipboardData.getData("text/plain")}n.val(i);t.preventDefault();return false}}).each(function(){var t=e(this).data("numFormat",{c:-(s+1),decimals:s,thousands_sep:u,dec_point:o,regex_dec_num:f,regex_dec:l,init:this.value.indexOf(".")?true:false});if(this.value==="")return;t.val(t.val())})}else{return this.each(function(){var t=e(this),n=+t.text().replace(f,"").replace(l,".");t.number(!isFinite(n)?0:+n,s,o,u)})}}return this.text(e.number.apply(window,arguments))};var i=null,s=null;if(e.isPlainObject(e.valHooks.text)){if(e.isFunction(e.valHooks.text.get))i=e.valHooks.text.get;if(e.isFunction(e.valHooks.text.set))s=e.valHooks.text.set}else{e.valHooks.text={}}e.valHooks.text.get=function(t){var n=e(t),r,s,o=n.data("numFormat");if(!o){if(e.isFunction(i)){return i(t)}else{return undefined}}else{if(t.value==="")return"";r=+t.value.replace(o.regex_dec_num,"").replace(o.regex_dec,".");return(t.value.indexOf("-")===0?"-":"")+(isFinite(r)?r:0)}};e.valHooks.text.set=function(t,n){var r=e(t),i=r.data("numFormat");if(!i){if(e.isFunction(s)){return s(t,n)}else{return undefined}}else{var o=e.number(n,i.decimals,i.dec_point,i.thousands_sep);return t.value=o}};e.number=function(e,t,n,r){r=typeof r==="undefined"?",":r;n=typeof n==="undefined"?".":n;t=!isFinite(+t)?0:Math.abs(t);var i="\\u"+("0000"+n.charCodeAt(0).toString(16)).slice(-4);var s="\\u"+("0000"+r.charCodeAt(0).toString(16)).slice(-4);e=(e+"").replace(".",n).replace(new RegExp(s,"g"),"").replace(new RegExp(i,"g"),".").replace(new RegExp("[^0-9+-Ee.]","g"),"");var o=!isFinite(+e)?0:+e,u="",a=function(e,t){var n=Math.pow(10,t);return""+Math.round(e*n)/n};u=(t?a(o,t):""+Math.round(o)).split(".");if(u[0].length>3){u[0]=u[0].replace(/\B(?=(?:\d{3})+(?!\d))/g,r)}if((u[1]||"").length",47:"?"}},f,i;n.fn.number=function(i,f,e,o){o=typeof o=="undefined"?",":o;e=typeof e=="undefined"?".":e;f=typeof f=="undefined"?0:f;var s="\\u"+("0000"+e.charCodeAt(0).toString(16)).slice(-4),h=new RegExp("[^"+s+"0-9]","g"),c=new RegExp(s,"g");return i===!0?this.is("input:text")?this.on({"keydown.format":function(i){var w=n(this),c=w.data("numFormat"),l=i.keyCode?i.keyCode:i.which,a="",s=r.apply(this,["start"]),y=r.apply(this,["end"]),p="",v=!1,h;if(u.codes.hasOwnProperty(l)&&(l=u.codes[l]),!i.shiftKey&&l>=65&&l<=90?l+=32:!i.shiftKey&&l>=69&&l<=105?l-=48:i.shiftKey&&u.shifts.hasOwnProperty(l)&&(a=u.shifts[l]),a==""&&(a=String.fromCharCode(l)),l!=8&&l!=45&&l!=127&&a!=e&&!a.match(/[0-9]/))return(h=i.keyCode?i.keyCode:i.which,h==46||h==8||h==127||h==9||h==27||h==13||(h==65||h==82||h==80||h==83||h==70||h==72||h==66||h==74||h==84||h==90||h==61||h==173||h==48)&&(i.ctrlKey||i.metaKey)===!0||(h==86||h==67||h==88)&&(i.ctrlKey||i.metaKey)===!0||h>=35&&h<=39||h>=112&&h<=123)?void 0:(i.preventDefault(),!1);if(s==0&&y==this.value.length||w.val()==0?l==8?(s=y=1,this.value="",c.init=f>0?-1:0,c.c=f>0?-(f+1):0,t.apply(this,[0,0])):a==e?(s=y=1,this.value="0"+e+new Array(f+1).join("0"),c.init=f>0?1:0,c.c=f>0?-(f+1):0):l==45?(s=y=2,this.value="-0"+e+new Array(f+1).join("0"),c.init=f>0?1:0,c.c=f>0?-(f+1):0,t.apply(this,[2,2])):(c.init=f>0?-1:0,c.c=f>0?-f:0):c.c=y-this.value.length,c.isPartialSelection=s==y?!1:!0,f>0&&a==e&&s==this.value.length-f-1)c.c++,c.init=Math.max(0,c.init),i.preventDefault(),v=this.value.length+c.c;else if(l==45&&(s!=0||this.value.indexOf("-")==0))i.preventDefault();else if(a==e)c.init=Math.max(0,c.init),i.preventDefault();else if(f>0&&l==127&&s==this.value.length-f-1)i.preventDefault();else if(f>0&&l==8&&s==this.value.length-f)i.preventDefault(),c.c--,v=this.value.length+c.c;else if(f>0&&l==127&&s>this.value.length-f-1){if(this.value==="")return;this.value.slice(s,s+1)!="0"&&(p=this.value.slice(0,s)+"0"+this.value.slice(s+1),w.val(p));i.preventDefault();v=this.value.length+c.c}else if(f>0&&l==8&&s>this.value.length-f){if(this.value==="")return;this.value.slice(s-1,s)!="0"&&(p=this.value.slice(0,s-1)+"0"+this.value.slice(s),w.val(p));i.preventDefault();c.c--;v=this.value.length+c.c}else l==127&&this.value.slice(s,s+1)==o?i.preventDefault():l==8&&this.value.slice(s-1,s)==o?(i.preventDefault(),c.c--,v=this.value.length+c.c):f>0&&s==y&&this.value.length>f+1&&s>this.value.length-f-1&&isFinite(+a)&&!i.metaKey&&!i.ctrlKey&&!i.altKey&&a.length===1&&(p=y===this.value.length?this.value.slice(0,s-1):this.value.slice(0,s)+this.value.slice(s+1),this.value=p,v=s);v!==!1&&t.apply(this,[v,v]);w.data("numFormat",c)},"keyup.format":function(i){var o=n(this),u=o.data("numFormat"),e=i.keyCode?i.keyCode:i.which,h=r.apply(this,["start"]),c=r.apply(this,["end"]),s;(h===0&&c===0&&(e===189||e===109)&&(o.val("-"+o.val()),h=1,u.c=1-this.value.length,u.init=1,o.data("numFormat",u),s=this.value.length+u.c,t.apply(this,[s,s])),this.value===""||(e<48||e>57)&&(e<96||e>105)&&e!==8&&e!==46&&e!==110)||(o.val(o.val()),f>0&&(u.init<1?(h=this.value.length-f-(u.init<0?1:0),u.c=h-this.value.length,u.init=1,o.data("numFormat",u)):h>this.value.length-f&&e!=8&&(u.c++,o.data("numFormat",u))),e!=46||u.isPartialSelection||(u.c++,o.data("numFormat",u)),s=this.value.length+u.c,t.apply(this,[s,s]))},"paste.format":function(t){var u=n(this),i=t.originalEvent,r=null;return window.clipboardData&&window.clipboardData.getData?r=window.clipboardData.getData("Text"):i.clipboardData&&i.clipboardData.getData&&(r=i.clipboardData.getData("text/plain")),u.val(r),t.preventDefault(),!1}}).each(function(){var t=n(this).data("numFormat",{c:-(f+1),decimals:f,thousands_sep:o,dec_point:e,regex_dec_num:h,regex_dec:c,init:this.value.indexOf(".")?!0:!1});this.value!==""&&t.val(t.val())}):this.each(function(){var t=n(this),i=+t.text().replace(h,"").replace(c,".");t.number(isFinite(i)?+i:0,f,e,o)}):this.text(n.number.apply(window,arguments))};f=null;i=null;n.isPlainObject(n.valHooks.text)?(n.isFunction(n.valHooks.text.get)&&(f=n.valHooks.text.get),n.isFunction(n.valHooks.text.set)&&(i=n.valHooks.text.set)):n.valHooks.text={};n.valHooks.text.get=function(t){var u=n(t),i,r=u.data("numFormat");return r?t.value===""?"":(i=+t.value.replace(r.regex_dec_num,"").replace(r.regex_dec,"."),(t.value.indexOf("-")===0?"-":"")+(isFinite(i)?i:0)):n.isFunction(f)?f(t):undefined};n.valHooks.text.set=function(t,r){var e=n(t),u=e.data("numFormat"),f;return u?(f=n.number(r,u.decimals,u.dec_point,u.thousands_sep),i(t,f)):n.isFunction(i)?i(t,r):undefined};n.number=function(n,t,i,r){var f,e;r=typeof r=="undefined"?",":r;i=typeof i=="undefined"?".":i;t=isFinite(+t)?Math.abs(t):0;f="\\u"+("0000"+i.charCodeAt(0).toString(16)).slice(-4);e="\\u"+("0000"+r.charCodeAt(0).toString(16)).slice(-4);n=(n+"").replace(".",i).replace(new RegExp(e,"g"),"").replace(new RegExp(f,"g"),".").replace(new RegExp("[^0-9+-Ee.]","g"),"");var o=isFinite(+n)?+n:0,u="",s=function(n,t){var i=Math.pow(10,t);return""+Math.round(n*i)/i};return u=(t?s(o,t):""+Math.round(o)).split("."),u[0].length>3&&(u[0]=u[0].replace(/\B(?=(?:\d{3})+(?!\d))/g,r)),(u[1]||"").length