Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add more than one kind of separator in numeric #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions numeric/jquery.numeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down