diff --git a/README.md b/README.md index 03ceef2..18ffe80 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,11 @@ allowFloat: (boolean) Allows floating point numbers to be entered. Default: fals allowNegative: (boolean) Allows negative numbers to be entered. Default: false. +allowCopy: (boolean) Allows copying the value of the input element. Default: true. + +allowPaste: (boolean) Allows pasting into the input element. Default: false. + +useCommaInsteadOfDot: (boolean) Allowd , and disallows . to be entered. Default: false. ### Usage ### @@ -42,4 +47,6 @@ To create an input element which accepts any number (positive or negative floati $("#elementID").numericInput({ allowFloat: true, allowNegative: true }); +To allow a comma instead of a dot as decimal separator set the `useCommaInsteadOfDot` option to `true` + $("#elementID").numericInput({ useCommaInsteadOfDot: true }); \ No newline at end of file diff --git a/numericInput.js b/numericInput.js index 26ede21..268dc90 100644 --- a/numericInput.js +++ b/numericInput.js @@ -2,8 +2,8 @@ // Author: Joshua De Leon // File: numericInput.js // Description: Allows only numeric input in an element. -// -// If you happen upon this code, enjoy it, learn from it, and +// +// If you happen upon this code, enjoy it, learn from it, and // if possible please credit me: www.transtatic.com /////////////////////////////////////////////////////////////// @@ -12,83 +12,119 @@ // Plugin defaults var defaults = { allowFloat: false, - allowNegative: false - }; - + allowNegative: false, + allowCopy: true, + allowPaste: false, + useCommaInsteadOfDot: false + }; + // Plugin definition // allowFloat: (boolean) Allows floating point (real) numbers. If set to false only integers will be allowed. Default: false. // allowNegative: (boolean) Allows negative values. If set to false only positive number input will be allowed. Default: false. - $.fn.numericInput = function( options ) { - var settings = $.extend( {}, defaults, options ); + // useCommaInsteadOfDot: (boolean) Allows , and disallows . to resemble European number notation. To save the input to a database or to make calculations, the comma should be replaced to a dot though. Default: false. + $.fn.numericInput = function( options ) { + var settings = $.extend( {}, defaults, options ); var allowFloat = settings.allowFloat; var allowNegative = settings.allowNegative; - + var allowCopy = settings.allowCopy; + var allowPaste = settings.allowPaste; + var useCommaInsteadOfDot = settings.useCommaInsteadOfDot; + + var decimalSeparator = (useCommaInsteadOfDot ? 44 : 46); + this.keypress(function (event) { var inputCode = event.which; var currentValue = $(this).val(); if (inputCode > 0 && (inputCode < 48 || inputCode > 57)) // Checks the if the character code is not a digit { - if (allowFloat == true && inputCode == 46) // Conditions for a period (decimal point) + if (allowFloat == true && inputCode == decimalSeparator) // Conditions for a period (decimal point) { //Disallows a period before a negative - if (allowNegative == true && getCaret(this) == 0 && currentValue.charAt(0) == '-') + if (allowNegative == true && getCaret(this) == 0 && currentValue.charAt(0) == '-') return false; //Disallows more than one decimal point. - if (currentValue.match(/[.]/)) - return false; + if (useCommaInsteadOfDot) + { + if (currentValue.match(/[,]/)) + return false; + } + else + { + if (currentValue.match(/[.]/)) + return false; + } } else if (allowNegative == true && inputCode == 45) // Conditions for a decimal point { - if(currentValue.charAt(0) == '-') + if(currentValue.charAt(0) == '-') + return false; + + if(getCaret(this) != 0) return false; - - if(getCaret(this) != 0) - return false; } else if (inputCode == 8) // Allows backspace - return true; + return true; + + else if (allowCopy && event.ctrlKey && inputCode == 99) // Allows ctrl-c + return true; + + else if (allowPaste && event.ctrlKey && inputCode == 118) // Allows ctrl-v + return true; else // Disallow non-numeric - return false; + return false; } else if(inputCode > 0 && (inputCode >= 48 && inputCode <= 57)) // Disallows numbers before a negative. { - if (allowNegative == true && currentValue.charAt(0) == '-' && getCaret(this) == 0) + if (allowNegative == true && currentValue.charAt(0) == '-' && getCaret(this) == 0) return false; } + + else if(inputCode == 0) { + // Special codes; allow everything (e.g. Ins/Del/Home/End/PgUp/PgDn or zoom/media buttons), but block Ctrl-Ins (copy) and Shift-Ins (paste) unless they are allowed + + if (event.ctrlKey && event.keyCode == 45 && !allowCopy) // Blocks ctrl-insert (copy) + return false; + + else if (event.shiftKey && event.keyCode == 45 && !allowPaste) // Blocks shift-insert (paste) + return false; + + else + return true; + } }); - + return this; }; - - + + // Private function for selecting cursor position. Makes IE play nice. // http://stackoverflow.com/questions/263743/how-to-get-caret-position-in-textarea - function getCaret(element) - { - if (element.selectionStart) - return element.selectionStart; + function getCaret(element) + { + if (element.selectionStart) + return element.selectionStart; else if (document.selection) //IE specific - { - element.focus(); + { + element.focus(); - var r = document.selection.createRange(); - if (r == null) - return 0; + var r = document.selection.createRange(); + if (r == null) + return 0; - var re = element.createTextRange(), - rc = re.duplicate(); - re.moveToBookmark(r.getBookmark()); - rc.setEndPoint('EndToStart', re); - return rc.text.length; - } + var re = element.createTextRange(), + rc = re.duplicate(); + re.moveToBookmark(r.getBookmark()); + rc.setEndPoint('EndToStart', re); + return rc.text.length; + } - return 0; + return 0; }; }( jQuery )); \ No newline at end of file diff --git a/numericInput.min.js b/numericInput.min.js index 5c9a9a4..a2127e9 100644 --- a/numericInput.min.js +++ b/numericInput.min.js @@ -1,2 +1,2 @@ // Author: Joshua De Leon - File: numericInput.js - Description: Allows only numeric input in an element. - If you happen upon this code, enjoy it, learn from it, and if possible please credit me: www.transtatic.com -(function(b){var c={allowFloat:false,allowNegative:false};b.fn.numericInput=function(e){var f=b.extend({},c,e);var d=f.allowFloat;var g=f.allowNegative;this.keypress(function(j){var i=j.which;var h=b(this).val();if(i>0&&(i<48||i>57)){if(d==true&&i==46){if(g==true&&a(this)==0&&h.charAt(0)=="-"){return false}if(h.match(/[.]/)){return false}}else{if(g==true&&i==45){if(h.charAt(0)=="-"){return false}if(a(this)!=0){return false}}else{if(i==8){return true}else{return false}}}}else{if(i>0&&(i>=48&&i<=57)){if(g==true&&h.charAt(0)=="-"&&a(this)==0){return false}}}});return this};function a(d){if(d.selectionStart){return d.selectionStart}else{if(document.selection){d.focus();var f=document.selection.createRange();if(f==null){return 0}var e=d.createTextRange(),g=e.duplicate();e.moveToBookmark(f.getBookmark());g.setEndPoint("EndToStart",e);return g.text.length}}return 0}}(jQuery)); \ No newline at end of file +(function(b){var c={allowFloat:false,allowNegative:false,allowCopy:true,allowPaste:false,useCommaInsteadOfDot:false};b.fn.numericInput=function(e){var g=b.extend({},c,e);var d=g.allowFloat;var j=g.allowNegative;var i=g.allowCopy;var k=g.allowPaste;var h=g.useCommaInsteadOfDot;var f=(h?44:46);this.keypress(function(n){var m=n.which;var l=b(this).val();if(m>0&&(m<48||m>57)){if(d==true&&m==f){if(j==true&&a(this)==0&&l.charAt(0)=="-"){return false}if(h){if(l.match(/[,]/)){return false}}else{if(l.match(/[.]/)){return false}}}else{if(j==true&&m==45){if(l.charAt(0)=="-"){return false}if(a(this)!=0){return false}}else{if(m==8){return true}else{if(i&&n.ctrlKey&&m==99){return true}else{if(k&&n.ctrlKey&&m==118){return true}else{return false}}}}}}else{if(m>0&&(m>=48&&m<=57)){if(j==true&&l.charAt(0)=="-"&&a(this)==0){return false}}else{if(m==0){if(n.ctrlKey&&n.keyCode==45&&!i){return false}else{if(n.shiftKey&&n.keyCode==45&&!k){return false}else{return true}}}}}});return this};function a(d){if(d.selectionStart){return d.selectionStart}else{if(document.selection){d.focus();var f=document.selection.createRange();if(f==null){return 0}var e=d.createTextRange(),g=e.duplicate();e.moveToBookmark(f.getBookmark());g.setEndPoint("EndToStart",e);return g.text.length}}return 0}}(jQuery)); \ No newline at end of file