Skip to content

Commit

Permalink
Merge pull request #99 from MaRoed/altDecimal-separator
Browse files Browse the repository at this point in the history
alternative decimal separator in configuration parameters
  • Loading branch information
SamWM committed May 9, 2017
2 parents 3b0e565 + 8961c1d commit 44b12fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
27 changes: 20 additions & 7 deletions numeric/jquery.numeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @example $(".numeric").numeric();
* @example $(".numeric").numeric(","); // use , as separator
* @example $(".numeric").numeric({ decimal : "," }); // use , as separator
* @example $(".numeric").numeric({ altDecimal : "," }); // accept , as alternative separator, but use . as separator in output
* @example $(".numeric").numeric({ negative : false }); // do not allow negative values
* @example $(".numeric").numeric({ decimalPlaces : 2 }); // only allow 2 decimal places
* @example $(".numeric").numeric(null, callback); // use default values, pass on the 'callback' function
Expand All @@ -43,22 +44,26 @@ $.fn.numeric = function(config, callback)
if(typeof config.negative == "undefined") { config.negative = true; }
// set decimal point
var decimal = (config.decimal === false) ? "" : config.decimal || ".";
// set alternative key as decimal point
var altDecimal = (config.altDecimal === false) ? "" : config.altDecimal || decimal;
// allow negatives
var negative = (config.negative === true) ? true : false;
// set decimal places
// set decimal places
var decimalPlaces = (typeof config.decimalPlaces == "undefined") ? -1 : config.decimalPlaces;
// callback function
callback = (typeof(callback) == "function" ? callback : function() {});
// set data and methods
return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.callback", callback).data("numeric.decimalPlaces", decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur);
return this.data("numeric.decimal", decimal).data("numeric.altDecimal", altDecimal).data("numeric.negative", negative).data("numeric.callback", callback).data("numeric.decimalPlaces", decimalPlaces).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur);
};

$.fn.numeric.keypress = function(e)
{
// get decimal character and determine if negatives are allowed
var decimal = $.data(this, "numeric.decimal");
var negative = $.data(this, "numeric.negative");
var decimalPlaces = $.data(this, "numeric.decimalPlaces");
var decimalPlaces = $.data(this, "numeric.decimalPlaces");
// get an alternative decimal separator
var altDecimal = $.data(this, "numeric.altDecimal");
// get the key that was pressed
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
// allow enter/return key (only when in an input box)
Expand Down Expand Up @@ -131,8 +136,8 @@ $.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 key pressed is the decimal or altDecimal and decimal is not already in the field
if(decimal && key == decimal.charCodeAt(0) || altDecimal && key == altDecimal.charCodeAt(0))
{
if($.inArray(decimal, value.split('')) == -1)
{
Expand Down Expand Up @@ -173,7 +178,9 @@ $.fn.numeric.keyup = function(e)
// get decimal character and determine if negatives are allowed
var decimal = $.data(this, "numeric.decimal");
var negative = $.data(this, "numeric.negative");
var decimalPlaces = $.data(this, "numeric.decimalPlaces");
var decimalPlaces = $.data(this, "numeric.decimalPlaces");
// get an alternative decimal separator
var altDecimal = $.data(this, "numeric.altDecimal");

// prepend a 0 if necessary
if(decimal !== "" && decimal !== null)
Expand Down Expand Up @@ -226,6 +233,12 @@ $.fn.numeric.keyup = function(e)
break;
}
}
// if not a valid character and character is altDecimal, replace
if(!validChar && ch == altDecimal)
{
val = val.substring(0, i) + decimal + val.substring(i + 1);
validChar = true;
}
// if not a valid character, or a space, remove
if(!validChar || ch == " ")
{
Expand Down Expand Up @@ -281,7 +294,7 @@ $.fn.numeric.blur = function()

$.fn.removeNumeric = function()
{
return this.data("numeric.decimal", null).data("numeric.negative", null).data("numeric.callback", null).data("numeric.decimalPlaces", null).unbind("keypress", $.fn.numeric.keypress).unbind("keyup", $.fn.numeric.keyup).unbind("blur", $.fn.numeric.blur);
return this.data("numeric.decimal", null).data("numeric.altDecimal", null).data("numeric.negative", null).data("numeric.callback", null).data("numeric.decimalPlaces", null).unbind("keypress", $.fn.numeric.keypress).unbind("keyup", $.fn.numeric.keyup).unbind("blur", $.fn.numeric.blur);
};

// Based on code from http://javascript.nwbox.com/cursor_position/ (Diego Perini <dperini@nwbox.com>)
Expand Down
2 changes: 1 addition & 1 deletion numeric/jquery.numeric.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions numeric/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
<br/><br/>
Numbers with up to 2 decimal places:
<input class="decimal-2-places" type="text" />
<br/><br/>
Alternative (,) changes to standard (.) decimal separator:
<input class="alternative-decimal-separator" type="text" />
<br/><br/>
Reverse change from alternative (.) to standard (,) decimal separator:
<input class="alternative-decimal-separator-reverse" type="text" />
<br/><br/>
<a href="#" id="remove">Remove numeric</a>
</form>
Expand All @@ -28,12 +34,14 @@
$(".integer").numeric(false, function() { alert("Integers only"); this.value = ""; this.focus(); });
$(".positive").numeric({ negative: false }, function() { alert("No negative values"); this.value = ""; this.focus(); });
$(".positive-integer").numeric({ decimal: false, negative: false }, function() { alert("Positive integers only"); this.value = ""; this.focus(); });
$(".decimal-2-places").numeric({ decimalPlaces: 2 });
$(".decimal-2-places").numeric({ decimalPlaces: 2 });
$(".alternative-decimal-separator").numeric({ altDecimal: "," });
$(".alternative-decimal-separator-reverse").numeric({ altDecimal: ".", decimal : "," });
$("#remove").click(
function(e)
{
e.preventDefault();
$(".numeric,.integer,.positive,.positive-integer,.decimal-2-places").removeNumeric();
$(".numeric,.integer,.positive,.positive-integer,.decimal-2-places,.alternative-decimal-separator,.alternative-decimal-separator-reverse").removeNumeric();
}
);
</script>
Expand Down

0 comments on commit 44b12fa

Please sign in to comment.