From fc0fde93d8d595c22ff0af545a63c32f1fff936c Mon Sep 17 00:00:00 2001
From: John Edmonds
Date: Sat, 2 Jul 2011 11:45:24 -0400
Subject: [PATCH 1/7] Added an option (precision) to set how many numbers can
appear after the decimal.
---
numeric/jquery.numeric.js | 43 ++++++++++++++++++++++++++++++++++++---
numeric/test.html | 7 +++++--
2 files changed, 45 insertions(+), 5 deletions(-)
diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js
index 3ce51cf..d0a646f 100644
--- a/numeric/jquery.numeric.js
+++ b/numeric/jquery.numeric.js
@@ -23,6 +23,8 @@
* @example $(".numeric").numeric({ decimal : "," }); // use , as separator
* @example $(".numeric").numeric({ negative : false }); // do not allow negative values
* @example $(".numeric").numeric(null, callback); // use default values, pass on the 'callback' function
+ * @example $(".numeric").numeric({ precision: 2 }); // allow only two numbers after the decimal point.
+ * @example $(".numeric").numeric({ precision: 0 }); // Same as $(".numeric").numeric({ decimal : false });
*
*/
$.fn.numeric = function(config, callback)
@@ -40,8 +42,24 @@ $.fn.numeric = function(config, callback)
var negative = (config.negative === true) ? true : false;
// callback function
var callback = typeof callback == "function" ? callback : function(){};
+ // precision
+ var precision;
+ if ((typeof config.precision) == "number")
+ {
+ if (config.precision == 0)
+ {
+ decimal = false;
+ precision = -1;
+ }
+ else
+ precision = config.precision;
+ }
+ else
+ precision = -1;
+
+ var precision = (typeof config.precision) == "number" ? config.precision : -1;
// set data and methods
- return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.callback", callback).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur);
+ return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.callback", callback).data("numeric.precision", precision).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur);
}
$.fn.numeric.keypress = function(e)
@@ -129,9 +147,23 @@ $.fn.numeric.keypress = function(e)
}
}
}
+ //if a number key was pressed.
else
{
- allow = true;
+ // If precision >= 0, make sure there's only characters
+ // after the decimal point.
+ if($.data(this, "numeric.precision") >= 0)
+ {
+ var decimalPosition = this.value.indexOf(decimal);
+ //If there is a decimal and the cursor is after the decimal.
+ if (decimalPosition >= 0
+ && $.fn.getSelectionStart(this) > decimalPosition)
+ allow = this.value.length - decimalPosition - 1 < $.data(this, "numeric.precision");
+ else
+ allow = true;
+ }
+ else
+ allow = true;
}
return allow;
}
@@ -213,7 +245,12 @@ $.fn.numeric.keyup = function(e)
val = val.substring(0, i) + val.substring(i + 1);
}
}
+ // remove numbers after the decimal so that precision matches.
+ if ($.data(this, "numeric.precision") >= 0)
+ val = val.substring(0, firstDecimal+$.data(this, "numeric.precision") + 1);
+
}
+
// set the value and prevent the cursor moving to the end
this.value = val;
$.fn.setSelection(this, carat);
@@ -276,4 +313,4 @@ $.fn.setSelection = function(o, p)
}
}
-})(jQuery);
\ No newline at end of file
+})(jQuery);
diff --git a/numeric/test.html b/numeric/test.html
index 631dbfc..b70bffb 100644
--- a/numeric/test.html
+++ b/numeric/test.html
@@ -14,6 +14,8 @@
No negative values (integer only):
+ Two decimal points:
+
Remove numeric