forked from customd/jquery-number
/
jquery.number.min.js
9 lines (9 loc) · 4.18 KB
/
jquery.number.min.js
1
2
3
4
5
6
7
8
9
(function(f){function p(d,c){if(this.createTextRange){var b=this.createTextRange();b.collapse(true);b.moveStart("character",d);b.moveEnd("character",c);b.select()}else if(this.setSelectionRange){this.focus();this.setSelectionRange(d,c)}}function q(d){var c=f(this).val(),b;b=this.value.length;d=d.toLowerCase()=="start"?"Start":"End";if(this.createTextRange){b=document.selection.createRange().duplicate();b["move"+d]("character",-c.length);b=c.lastIndexOf(b.text)}else if(typeof this["selection"+d]!=
"undefined")b=this["selection"+d];return b}f.fn.number=function(d,c,b,k){k=typeof k==="undefined"?",":k;b=typeof b==="undefined"?".":b;var h="\\u"+("0000"+b.charCodeAt(0).toString(16)).substr(-4),m=RegExp("[^"+h+"0-9]","g"),n=RegExp(h,"g");if(d===true)return this.is("input:text")?this.on({"keydown.format":function(a){var g=f(this),e=g.data("numFormat"),i=a.keyCode?a.keyCode:a.which,l=unescape(a.originalEvent.keyIdentifier.replace("U+","%u")),j=q.apply(this,["start"]),o=q.apply(this,["end"]),r="";
setPos=false;if(!(i!==8&&l!=b&&(i<48||i>57)&&(i<96||i>105))){if((j==0&&o==this.value.length||g.val()==0)&&!a.metaKey&&!a.ctrlKey&&!a.altKey&&l.length===1&&l!=0){j=o=1;this.value="";e.init=-1;e.c=-(c+1);p.apply(this,[0,0])}else e.c=o-this.value.length;if(j==this.value.length-c-1&&l==b){e.c++;e.init=Math.max(0,e.init);a.preventDefault();setPos=this.value.length+e.c}else if(l==b){e.init=Math.max(0,e.init);a.preventDefault()}else if(i==8&&j==this.value.length-c){a.preventDefault();e.c--;setPos=this.value.length+
e.c}else if(i==8&&j>this.value.length-c){if(this.value==="")return;if(this.value.substr(j-1,1)!="0"){r=this.value.substr(0,j-1)+"0"+this.value.substr(j);g.val(r.replace(m,"").replace(n,b))}a.preventDefault();e.c--;setPos=this.value.length+e.c}else if(i==8&&this.value.substr(j-1,1)==k){a.preventDefault();e.c--;setPos=this.value.length+e.c}else if(j==o&&this.value.length>c+1&&j>this.value.length-c-1&&isFinite(+l)&&!a.metaKey&&!a.ctrlKey&&!a.altKey&&l.length===1){this.value=r=o===this.value.length?this.value.substr(0,
j-1):this.value.substr(0,j)+this.value.substr(j+1);setPos=j}setPos!==false&&p.apply(this,[setPos,setPos]);g.data("numFormat",e)}},"keyup.format":function(a){var g=f(this),e=g.data("numFormat");a=a.keyCode?a.keyCode:a.which;var i=q.apply(this,["start"]);if(!(this.value===""||(a<48||a>57)&&(a<96||a>105)&&a!==8)){g.val(g.val());if(e.init<1){i=this.value.length-c-(e.init<0?1:0);e.c=i-this.value.length;e.init=1;g.data("numFormat",e)}else if(i>this.value.length-c&&a!=8){e.c++;g.data("numFormat",e)}g=this.value.length+
e.c;p.apply(this,[g,g])}},"paste.format":function(a){var g=f(this),e=a.originalEvent,i=null;if(window.clipboardData&&window.clipboardData.getData)i=window.clipboardData.getData("Text");else if(e.clipboardData&&e.clipboardData.getData)i=e.clipboardData.getData("text/plain");g.val(i);a.preventDefault();return false}}).each(function(){var a=f(this).data("numFormat",{c:-(c+1),decimals:c,thousands_sep:k,dec_point:b,regex_dec_num:m,regex_dec:n,init:false});this.value!==""&&a.val(a.val())}):this.each(function(){var a=
f(this),g=+a.text().replace(m,"").replace(n,".");a.number(!isFinite(g)?0:+g,c,b,k)});return this.text(f.number.apply(window,arguments))};var s=null,t=null;if(f.valHooks.text){s=f.valHooks.text.get;t=f.valHooks.text.set}else f.valHooks.text={};f.valHooks.text.get=function(d){var c=f(d).data("numFormat");if(c){if(d.value==="")return"";d=+d.value.replace(c.regex_dec_num,"").replace(c.regex_dec,c.dec_point);return""+(isFinite(d)?d:0)}else if(f.isFunction(s))return s(d)};f.valHooks.text.set=function(d,
c){var b=f(d).data("numFormat");if(b)return d.value=f.number(c,b.decimals,b.dec_point,b.thousands_sep);else if(f.isFunction(t))return t(d,c)};f.number=function(d,c,b,k){k=typeof k==="undefined"?",":k;b=typeof b==="undefined"?".":b;c=!isFinite(+c)?0:Math.abs(c);var h="\\u"+("0000"+b.charCodeAt(0).toString(16)).substr(-4);d=(d+"").replace(RegExp(h,"g"),".").replace(RegExp("[^0-9+-Ee.]","g"),"");d=!isFinite(+d)?0:+d;h="";h=function(m,n){var a=Math.pow(10,n);return""+Math.round(m*a)/a};h=(c?h(d,c):""+
Math.round(d)).split(".");if(h[0].length>3)h[0]=h[0].replace(/\B(?=(?:\d{3})+(?!\d))/g,k);if((h[1]||"").length<c){h[1]=h[1]||"";h[1]+=Array(c-h[1].length+1).join("0")}return h.join(b)}})(jQuery);