forked from customd/jquery-number
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.number.min.js
11 lines (11 loc) · 4.84 KB
/
jquery.number.min.js
1
2
3
4
5
6
7
8
9
10
11
/*! jQuery number 2.0.4 (c) github.com/teamdf/jquery-number | opensource.teamdf.com/license */
(function(g){function q(d,b){if(this.createTextRange){var c=this.createTextRange();c.collapse(true);c.moveStart("character",d);c.moveEnd("character",b-d);c.select()}else if(this.setSelectionRange){this.focus();this.setSelectionRange(d,b)}}function r(d){var b=this.value.length;d=d.toLowerCase()=="start"?"Start":"End";if(document.selection){b=document.selection.createRange();var c;c=b.duplicate();c.expand("textedit");c.setEndPoint("EndToEnd",b);c=c.text.length-b.text.length;b=c+b.text.length;return d==
"Start"?c:b}else if(typeof this["selection"+d]!="undefined")b=this["selection"+d];return b}var p={codes:{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:"?"}};g.fn.number=function(d,b,c,k){k=typeof k==="undefined"?",":k;c=typeof c==="undefined"?".":c;var i="\\u"+("0000"+c.charCodeAt(0).toString(16)).slice(-4),
m=RegExp("[^"+i+"0-9]","g"),n=RegExp(i,"g");if(d===true)return this.is("input:text")?this.on({"keydown.format":function(a){var h=g(this),e=h.data("numFormat"),f=a.keyCode?a.keyCode:a.which,l="",j=r.apply(this,["start"]),o=r.apply(this,["end"]),s="";setPos=false;if(typeof a.originalEvent.keyIdentifier!=="undefined")l=unescape(a.originalEvent.keyIdentifier.replace("U+","%u"));else{if(p.codes.hasOwnProperty(f))f=p.codes[f];if(!a.shiftKey&&f>=65&&f<=90)f+=32;else if(!a.shiftKey&&f>=69&&f<=105)f-=48;else if(a.shiftKey&&
p.shifts.hasOwnProperty(f))l=p.shifts[f];if(l=="")l=String.fromCharCode(f)}if(!(f!==8&&l!=c&&(f<48||f>57)&&(f<96||f>105))){if((j==0&&o==this.value.length||h.val()==0)&&!a.metaKey&&!a.ctrlKey&&!a.altKey&&l.length===1&&l!=0){j=o=1;this.value="";e.init=-1;e.c=-(b+1);q.apply(this,[0,0])}else e.c=o-this.value.length;if(l==c&&j==this.value.length-b-1){e.c++;e.init=Math.max(0,e.init);a.preventDefault();setPos=this.value.length+e.c}else if(l==c){e.init=Math.max(0,e.init);a.preventDefault()}else if(f==8&&
j==this.value.length-b){a.preventDefault();e.c--;setPos=this.value.length+e.c}else if(f==8&&j>this.value.length-b){if(this.value==="")return;if(this.value.slice(j-1,j)!="0"){s=this.value.slice(0,j-1)+"0"+this.value.slice(j);h.val(s.replace(m,"").replace(n,c))}a.preventDefault();e.c--;setPos=this.value.length+e.c}else if(f==8&&this.value.slice(j-1,j)==k){a.preventDefault();e.c--;setPos=this.value.length+e.c}else if(j==o&&this.value.length>b+1&&j>this.value.length-b-1&&isFinite(+l)&&!a.metaKey&&!a.ctrlKey&&
!a.altKey&&l.length===1){this.value=s=o===this.value.length?this.value.slice(0,j-1):this.value.slice(0,j)+this.value.slice(j+1);setPos=j}setPos!==false&&q.apply(this,[setPos,setPos]);h.data("numFormat",e)}},"keyup.format":function(a){var h=g(this),e=h.data("numFormat");a=a.keyCode?a.keyCode:a.which;var f=r.apply(this,["start"]);if(!(this.value===""||(a<48||a>57)&&(a<96||a>105)&&a!==8)){h.val(h.val());if(e.init<1){f=this.value.length-b-(e.init<0?1:0);e.c=f-this.value.length;e.init=1;h.data("numFormat",
e)}else if(f>this.value.length-b&&a!=8){e.c++;h.data("numFormat",e)}h=this.value.length+e.c;q.apply(this,[h,h])}},"paste.format":function(a){var h=g(this),e=a.originalEvent,f=null;if(window.clipboardData&&window.clipboardData.getData)f=window.clipboardData.getData("Text");else if(e.clipboardData&&e.clipboardData.getData)f=e.clipboardData.getData("text/plain");h.val(f);a.preventDefault();return false}}).each(function(){var a=g(this).data("numFormat",{c:-(b+1),decimals:b,thousands_sep:k,dec_point:c,
regex_dec_num:m,regex_dec:n,init:false});this.value!==""&&a.val(a.val())}):this.each(function(){var a=g(this),h=+a.text().replace(m,"").replace(n,".");a.number(!isFinite(h)?0:+h,b,c,k)});return this.text(g.number.apply(window,arguments))};var t=null,u=null;if(g.valHooks.text){t=g.valHooks.text.get;u=g.valHooks.text.set}else g.valHooks.text={};g.valHooks.text.get=function(d){var b=g(d).data("numFormat");if(b){if(d.value==="")return"";d=+d.value.replace(b.regex_dec_num,"").replace(b.regex_dec,".");
return""+(isFinite(d)?d:0)}else if(g.isFunction(t))return t(d)};g.valHooks.text.set=function(d,b){var c=g(d).data("numFormat");if(c)return d.value=g.number(b,c.decimals,c.dec_point,c.thousands_sep);else if(g.isFunction(u))return u(d,b)};g.number=function(d,b,c,k){k=typeof k==="undefined"?",":k;c=typeof c==="undefined"?".":c;b=!isFinite(+b)?0:Math.abs(b);var i="\\u"+("0000"+c.charCodeAt(0).toString(16)).slice(-4);d=(d+"").replace(RegExp(i,"g"),".").replace(RegExp("[^0-9+-Ee.]","g"),"");d=!isFinite(+d)?
0:+d;i="";i=function(m,n){var a=Math.pow(10,n);return""+Math.round(m*a)/a};i=(b?i(d,b):""+Math.round(d)).split(".");if(i[0].length>3)i[0]=i[0].replace(/\B(?=(?:\d{3})+(?!\d))/g,k);if((i[1]||"").length<b){i[1]=i[1]||"";i[1]+=Array(b-i[1].length+1).join("0")}return i.join(c)}})(jQuery);