This is a migrated thread and some comments may be shown as answers.

Get number of decimal places on keypress

1 Answer 407 Views
NumericTextBox
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 24 Feb 2017, 07:58 PM

We generate dynamic fields where the user can define their precision (total number of digits) and scale (number of digits to the right of the decimal).  We set max length based on precision + 1 for a negative and + 1 for the decimal such that if the user defined a field with precision 7 and scale of 3, the max length would be 9 (7 digits, 1 for potential and 1 for potential decimal).  (When the scale is 0, the max length is precision + 1 for potential negative).  We need to prevent users from entering more than the precision (e.g., the total number of digits) when entering values, regardless of whether the number is positive or negative as well as whether there's a decimal or not.  For example, if the precision is 5 and scale is 3, max length would be 7.  12345 would be valid, as would -12345 or -123.45.  However, 123456 or 12345.6 would not.

I have the following wired up (and executing upon keypress), but in traversing the sender object, I am unable to get to the options (where I believe the scale is kept based on this page):

$("#OBJ_MASKPERCENTAGEP5S3").kendoNumericTextBox({
    format: "#.### \\%",
    decimals:3
});
 
$("#OBJ_MASKPERCENTAGEP5S3").keypress(function(){
    checkNumericTextboxDecimals(this, null);
});
 
function checkNumericTextboxDecimals(sender, args) {
    window.setTimeout(function () {
    var textBox = $(sender.id).kendoNumericTextBox();

 

With the above in mind, given a Kendo Numeric Textbox, how does one go about getting the scale via jQuery?  

(Something similar to the above worked great when we had RadControls implemented, but isn't functional using Kendo controls.)

Finally, why isn't this functionality supported within the control itself?  I find it hard to believe we're the only ones who see this as a shortcoming.

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 28 Feb 2017, 10:49 PM
Hello Matt,

The NumericTextBox exposes "decimals", "min", "max" and "factor" properties for limiting the input and I have to say that the requirement that you have is not common. Nevertheless, for performing your custom validation you bind a keydown event using jQuery directly:
<input id="numerictextbox" />
<script>
$("#numerictextbox").kendoNumericTextBox({
    decimals: 1
});
   
  $("#numerictextbox").bind("keypress", function(){
    var numeric = $(this).data("kendoNumericTextBox");   
  })
</script>

Hope this helps.


Regards,
Konstantin Dikov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
NumericTextBox
Asked by
Matt
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or