/** * autoNumeric.js * @author: Bob Knothe * @version: 1.6.2 * * Created by Robert J. Knothe on 2010-10-25. Please report any bug at http://www.decorplanit.com/plugin/ * * Copyright (c) 2010 Robert J. Knothe http://www.decorplanit.com/plugin/ * * The MIT License (http://www.opensource.org/licenses/mit-license.php) */ (function(e){e.fn.autoNumeric=function(g){var h=e.extend({},e.fn.autoNumeric.defaults,g);return this.each(function(){var m=e(this);var u=this.id;var p=e.metadata?e.extend({},h,m.metadata()):h;p.mDec=isNaN(p.mDec*1)?e("#"+p.mDec).val()*1:p.mDec*1;var o="";var s=0;var n=0;var l=0;var i=0;var r=0;var t=0;var q=false;e(this).keydown(function(w){p=e.metadata?e.extend({},h,m.metadata()):h;p.mDec=isNaN(p.mDec*1)?e("#"+p.mDec).val()*1:p.mDec*1;q=false;if(!w){w=window.event}if(w.keyCode){o=w.keyCode}else{if(w.which){o=w.which}}if(w.metaKey){q=true}if(document.selection){this.focus();var v=document.selection.createRange();s=document.selection.createRange().text.length;v.moveStart("character",-this.value.length);n=(v.text.length-s)*1}else{if(this.selectionStart||this.selectionStart=="0"){s=this.selectionEnd*1-this.selectionStart*1;n=this.selectionStart*1}}l=this.value.length}).keypress(function(y){var A=p.aNum+p.aNeg+p.aDec;i=(this.value.lastIndexOf(p.aDec)==-1)?l:l-(l-this.value.lastIndexOf(p.aDec));r=f(this.value,0,i);if(this.value.lastIndexOf(p.aDec)!=-1){t=f(this.value,i,l)}if((y.ctrlKey||q)&&(o==65||o==67||o==86||o==88)){return true}if(o==8||o==9||o==13||o==35||o==36||o==37||o==39||o==46){return true}var z="";if(!y){y=window.event}if(y.keyCode){z=y.keyCode}else{if(y.which){z=y.which}}var x=String.fromCharCode(z);if(A.indexOf(x)==-1){y.preventDefault()}if(x==p.aDec){if(s==l&&s>0){return}if(n<=this.value.lastIndexOf("-")||this.value.indexOf(p.aDec)!=-1||p.mDec===0){y.preventDefault()}if(n<=this.value.lastIndexOf(p.aSep)&&this.value.lastIndexOf(p.aSep)!=-1&&p.aSep!==""){if(p.pSign=="s"&&p.aSign.indexOf(" ")>=0){var w=this.value.substring(0,this.value.length-p.aSign.length);var v=w.lastIndexOf(" ");if(n>v&&n>=w.length-p.mDec){return}else{y.preventDefault()}}else{y.preventDefault()}}if(p.aSign===""&&np.mDec||nthis.value.length-p.aSign.length||n0||this.value.indexOf("-")!=-1||p.aNeg==="")){if(s>=1&&n===0){return}else{y.preventDefault()}}if(z>=48&&z<=57){if(s>0){return}if(n0){y.preventDefault()}if(n>this.value.length-p.aSign.length&&p.aSign!==""&&p.pSign=="s"&&this.value!==""){y.preventDefault()}if(n==this.value.lastIndexOf("-")){y.preventDefault()}if(r>=p.mNum&&n<=i){y.preventDefault()}if(this.value.indexOf(p.aDec)!=-1&&n>=i+1&&t>=p.mDec){y.preventDefault()}}}).keyup(function(y){if(this.value===""){return}if(p.aSep===""||y.keyCode==9||y.keyCode==20||y.keyCode==35||y.keyCode==36||y.keyCode==37||y.keyCode==39||o==9||o==13||o==20||o==35||o==36||o==37||o==39){return}e(b(u)).val(a(this.value,p));var w=this.value.length;i=(this.value.lastIndexOf(p.aDec)==-1)?w:w-(w-this.value.lastIndexOf(p.aDec));r=f(this.value,0,i);if(r>p.mNum){e(b(u)).val("")}var v=0;if(lw){if(s===0){if((l-2)==w){if(o==8){v=(n-2)}else{if(o==46){v=n}else{v=(n-1)}}}else{v=(o==8)?n-1:n}}if(s>0&&s=0){v=n+s}if(this.value.charAt(n-1)==p.aSep&&o==8){v=(n-1)}else{if(this.value.charAt(n)==p.aSep&&o==46){v=(n+1)}}}var z=this;z.focus();if(document.selection){var x=z.createTextRange();x.collapse(true);x.moveStart("character",v);x.moveEnd("character",0);x.select()}else{if(z.selectionStart||z.selectionStart=="0"){z.selectionStart=v;z.selectionEnd=v}}}).bind("change focusout",function(){if(e(b(u)).val()!==""){c(m,u,p)}}).bind("paste",function(){setTimeout(function(){c(m,u,p)},0)})})};function b(g){g=g.replace(/\[/g,"\\[").replace(/\]/g,"\\]");return"#"+g.replace(/(:|\.)/g,"\\$1")}function f(l,m,g){var h="";var i=0;for(j=m;j="0"&&h<="9"){i++}}return i}function a(h,m){if(m.aSep!==""){var g="";if(m.dGroup==2){g=/(\d)((\d)(\d{2}?)+)$/}else{if(m.dGroup==4){g=/(\d)((\d{4}?)+)$/}else{g=/(\d)((\d{3}?)+)$/}}for(k=0;k1){h=i+m.aDec+l[1]}else{h=i}if(h.indexOf("-")!==-1&&m.aSign!==""&&m.pSign=="p"){h=h.replace("-","");return"-"+m.aSign+h}else{if(h.indexOf("-")==-1&&m.aSign!==""&&m.pSign=="p"){return m.aSign+h}}if(h.indexOf("-")!==-1&&m.aSign!==""&&m.pSign=="s"){h=h.replace("-","");return"-"+h+m.aSign}else{if(h.indexOf("-")==-1&&m.aSign!==""&&m.pSign=="s"){return h+m.aSign}else{return h}}}else{return h}}function d(n,s,h,u){n=(n==="")?"0":n+="";var g="";var p=0;var v="";if(n.charAt(0)=="-"){v=(n*1===0)?"":"-";n=n.replace("-","")}if((n*1)>0){while(n.substr(0,1)=="0"&&n.length>1){n=n.substr(1)}}var r=n.lastIndexOf(".");if(r===0){n="0"+n;r=1}if(r==-1||r==n.length-1){if(u&&s>0){g=(r==-1)?n+".":n;for(p=0;p4&&h==="S")||(m>4&&h==="A"&&v==="")||(m>5&&h==="A"&&v=="-")||(m>5&&h==="s")||(m>5&&h==="a"&&v==="")||(m>4&&h==="a"&&v=="-")||(m>5&&h==="B")||(m==5&&h==="B"&&t==1)||(m>0&&h==="C"&&v==="")||(m>0&&h==="F"&&v=="-")||(m>0&&h==="U")){for(p=(o.length-1);p>=0;p--){if(o[p]=="."){continue}o[p]++;if(o[p]<10){break}}}for(p=0;p<=l;p++){if(o[p]=="."||o[p]<10||p===0){g+=o[p]}else{g+="0"}}if(s===0){g=g.replace(".","")}return v+g}function c(l,q,n){l=l.val();if(l.length>100){e(b(q)).val("");return}var u="";if(n.aNeg=="-"){u="\\-"}var h=new RegExp("[^"+u+n.aNum+n.aDec+"]","gi");var g=l.replace(h,"");if(g.lastIndexOf("-")>0||g.indexOf(n.aDec)!=g.lastIndexOf(n.aDec)){g=""}var p="";var o=0;var t="";var m=0;var r=g.split("");for(m=0;m=0||r[m+1]<=9)){continue}else{p=p+r[m]}}p=t+p;if(p.indexOf(n.aDec)==-1&&p.length>(n.mNum+o)){p=""}if(p.indexOf(n.aDec)>(n.mNum+o)){p=""}if(p.indexOf(n.aDec)!=-1&&(n.aDec!=".")){p=p.replace(n.aDec,".")}p=d(p,n.mDec,n.mRound,n.aPad);if(n.aDec!="."){p=p.replace(".",n.aDec)}if(p!==""&&n.aSep!==""){p=a(p,n)}e(b(q)).val(p);return false}e.fn.autoNumeric.Strip=function(l,h){var m=e.extend({},e.fn.autoNumeric.defaults,h);var o=e.metadata?e.extend({},m,e(b(l)).metadata()):m;o.mDec=isNaN(o.mDec*1)?e("#"+o.mDec).val()*1:o.mDec*1;var g=e(b(l)).val();g=g.replace(o.aSign,"").replace("\u00A0","");var i=new RegExp("[^\\-"+o.aNum+o.aDec+"]","gi");g=g.replace(i,"");var n="";if(g.charAt(0)=="-"){n=(g*1===0)?"":"-";g=g.replace("-","")}g=g.replace(o.aDec,".");if(g*1>0){while(g.substr(0,1)=="0"&&g.length>1){g=g.substr(1)}}g=(g.lastIndexOf(".")===0)?("0"+g):g;g=(g*1===0)?"0":g;return n+g};e.fn.autoNumeric.Format=function(i,h,g){h+="";var m=e.extend({},e.fn.autoNumeric.defaults,g);var n=e.metadata?e.extend({},m,e(b(i)).metadata()):m;n.mDec=isNaN(n.mDec*1)?e("#"+n.mDec).val()*1:n.mDec*1;h=d(h,n.mDec,n.mRound,n.aPad);var l=0;if(h.indexOf("-")!=-1&&n.aNeg===""){h=""}else{if(h.indexOf("-")!=-1&&n.aNeg=="-"){l=1}}if(h.indexOf(".")==-1&&h.length>(n.mNum+l)){h=""}else{if(h.indexOf(".")>(n.mNum+l)){h=""}}if(n.aDec!="."){h=h.replace(".",n.aDec)}return a(h,n)};e.fn.autoNumeric.defaults={aNum:"0123456789",aNeg:"",aSep:",",aDec:".",aSign:"",pSign:"p",mNum:9,mDec:2,dGroup:3,mRound:"S",aPad:true}})(jQuery);