/* * * Copyright (c) 2006-2011 Sam Collett (http://www.texotela.co.uk) * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses. * * Version 1.3.1 * Demo: http://www.texotela.co.uk/code/jquery/numeric/ * */(function(e){e.fn.numeric=function(t,n){typeof t=="boolean"&&(t={decimal:t}),t=t||{},typeof t.negative=="undefined"&&(t.negative=!0);var r=t.decimal===!1?"":t.decimal||".",i=t.negative===!0?!0:!1;return n=typeof n=="function"?n:function(){},this.data("numeric.decimal",r).data("numeric.negative",i).data("numeric.callback",n).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"),r=e.data(this,"numeric.negative"),i=t.charCode?t.charCode:t.keyCode?t.keyCode:0;if(i==13&&this.nodeName.toLowerCase()=="input")return!0;if(i==13)return!1;var s=!1;if(t.ctrlKey&&i==97||t.ctrlKey&&i==65)return!0;if(t.ctrlKey&&i==120||t.ctrlKey&&i==88)return!0;if(t.ctrlKey&&i==99||t.ctrlKey&&i==67)return!0;if(t.ctrlKey&&i==122||t.ctrlKey&&i==90)return!0;if(t.ctrlKey&&i==118||t.ctrlKey&&i==86||t.shiftKey&&i==45)return!0;if(i<48||i>57){var o=e(this).val();if(!(o.indexOf("-")===0||!r||i!=45||o.length!==0&&parseInt(e.fn.getSelectionStart(this),10)!==0))return!0;n&&i==n.charCodeAt(0)&&o.indexOf(n)!=-1&&(s=!1),i!=8&&i!=9&&i!=13&&i!=35&&i!=36&&i!=37&&i!=39&&i!=46?s=!1:typeof t.charCode!="undefined"&&(t.keyCode==t.which&&t.which!==0?(s=!0,t.which==46&&(s=!1)):t.keyCode!==0&&t.charCode===0&&t.which===0&&(s=!0)),n&&i==n.charCodeAt(0)&&(o.indexOf(n)==-1?s=!0:s=!1)}else s=!0;return s},e.fn.numeric.keyup=function(t){var n=e(this).val();if(n&&n.length>0){var r=e.fn.getSelectionStart(this),i=e.fn.getSelectionEnd(this),s=e.data(this,"numeric.decimal"),o=e.data(this,"numeric.negative");if(s!==""&&s!==null){var u=n.indexOf(s);u===0&&(this.value="0"+n),u==1&&n.charAt(0)=="-"&&(this.value="-0"+n.substring(1)),n=this.value}var a=[0,1,2,3,4,5,6,7,8,9,"-",s],f=n.length;for(var l=f-1;l>=0;l--){var c=n.charAt(l);l!==0&&c=="-"?n=n.substring(0,l)+n.substring(l+1):l===0&&!o&&c=="-"&&(n=n.substring(1));var h=!1;for(var p=0;p0)for(var v=f-1;v>d;v--){var m=n.charAt(v);m==s&&(n=n.substring(0,v)+n.substring(v+1))}this.value=n,e.fn.setSelection(this,[r,i])}},e.fn.numeric.blur=function(){var t=e.data(this,"numeric.decimal"),n=e.data(this,"numeric.callback"),r=this.value;if(r!==""){var i=new RegExp("^\\d+$|^\\d*"+t+"\\d+$");i.exec(r)||n.apply(this)}},e.fn.removeNumeric=function(){return this.data("numeric.decimal",null).data("numeric.negative",null).data("numeric.callback",null).unbind("keypress",e.fn.numeric.keypress).unbind("blur",e.fn.numeric.blur)},e.fn.getSelectionStart=function(e){if(e.createTextRange){var t=document.selection.createRange().duplicate();return t.moveEnd("character",e.value.length),t.text===""?e.value.length:e.value.lastIndexOf(t.text)}return e.selectionStart},e.fn.getSelectionEnd=function(e){if(e.createTextRange){var t=document.selection.createRange().duplicate();return t.moveStart("character",-e.value.length),t.text.length}return e.selectionEnd},e.fn.setSelection=function(e,t){typeof t=="number"&&(t=[t,t]);if(t&&t.constructor==Array&&t.length==2)if(e.createTextRange){var n=e.createTextRange();n.collapse(!0),n.moveStart("character",t[0]),n.moveEnd("character",t[1]),n.select()}else e.setSelectionRange&&(e.focus(),e.setSelectionRange(t[0],t[1]))}})(jQuery);