diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js index 417f26c..73144ba 100644 --- a/numeric/jquery.numeric.js +++ b/numeric/jquery.numeric.js @@ -34,8 +34,8 @@ $.fn.numeric = function(config, callback) config = config || {}; // if config.negative undefined, set to true (default is to allow negative numbers) if(typeof config.negative == "undefined") { config.negative = true; } - // set decimal point - var decimal = (config.decimal === false) ? "" : config.decimal || "."; + // set decimal point to array + var decimal = (config.decimal === false) ? [] : typeof config.decimal ==="string"? [config.decimal]: config.decimal || ["."]; // allow negatives var negative = (config.negative === true) ? true : false; // callback function @@ -78,8 +78,9 @@ $.fn.numeric.keypress = function(e) var value = $(this).val(); /* '-' only allowed at start and if negative numbers allowed */ if(value.indexOf("-") !== 0 && negative && key == 45 && (value.length === 0 || parseInt($.fn.getSelectionStart(this), 10) === 0)) { return true; } - /* only one decimal separator allowed */ - if(decimal && key == decimal.charCodeAt(0) && value.indexOf(decimal) != -1) + /* only one decimal separator allowed */ + //La 3e condition est un intersection entre 2 array + if(decimal && decimal.indexOf(String.fromCharCode(key)) >= 0 && value.split('').filter(function(x){return decimal.indexOf(x) != -1}).length<=1) { allow = false; } @@ -118,9 +119,9 @@ $.fn.numeric.keypress = function(e) } } // if key pressed is the decimal and it is not already in the field - if(decimal && key == decimal.charCodeAt(0)) + if (decimal && decimal.indexOf(String.fromCharCode(key))>=0) { - if(value.indexOf(decimal) == -1) + if (value.split('').filter(function (x) { return decimal.indexOf(x) != -1 }).length === 0) { allow = true; } @@ -139,7 +140,7 @@ $.fn.numeric.keypress = function(e) $.fn.numeric.keyup = function(e) { - var val = $(this).val(); + var val = $(this).value; if(val && val.length > 0) { // get carat (cursor) position @@ -167,7 +168,7 @@ $.fn.numeric.keyup = function(e) } // if pasted in, only allow the following characters - var validChars = [0,1,2,3,4,5,6,7,8,9,'-',decimal]; + var validChars = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '-'].concat(decimal); // get length of the value (to loop through) var length = val.length; // loop backwards (to prevent going out of bounds) @@ -228,7 +229,7 @@ $.fn.numeric.blur = function() var val = this.value; if(val !== "") { - var re = new RegExp("^\\d+$|^\\d*" + decimal + "\\d+$"); + var re = new RegExp("^\\d+$|^\\d*" + decimal.join("|") + "\\d+$"); if(!re.exec(val)) { callback.apply(this);