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

DecimalDigits not working in RadNumericTextBox

3 Answers 155 Views
Input
This is a migrated thread and some comments may be shown as answers.
alphonse joseph
Top achievements
Rank 1
alphonse joseph asked on 16 Feb 2010, 08:35 AM
Hi,
  I am using RadNumericTextBox inside EditItemTemplate of a RadGrid.  I have given DecimalDigits property of RadNumericTextBox as "2".  But it accepts more than 2 decimal places depending on the MaxLength property.  How can I restrict the decimal places only to 2 digits?  Code I have used given below:

 <telerik:GridTemplateColumn  HeaderText="Debit" UniqueName="debit" DataType="System.Decimal" ItemStyle-Width="100px" ItemStyle-HorizontalAlign="Right" >
                        <ItemTemplate>
                        <asp:Label ID="lblDbAmt" Text= '<%# Eval("dbamt") %>' CssClass="labels"   runat="server"> </asp:Label>
                        </ItemTemplate>
           
                       <EditItemTemplate>
                               <telerik:RadNumericTextBox ID="txtDbAmt" Text  = '<%# bind("dbamt") %>' MinValue="0" MaxValue="99999999" MaxLength="11" DataType="System.Decimal" Culture="en-GB"  AutoPostBack="false" ClientEvents-OnKeyPress="KeyPressed" EnabledStyle-HorizontalAlign="Right" runat="server">                   
                                           <NumberFormat AllowRounding="false" DecimalDigits="2" DecimalSeparator="." GroupSeparator="," GroupSizes="3" KeepNotRoundedValue="true"   />
                               </telerik:RadNumericTextBox>   
                       </EditItemTemplate>

                    </telerik:GridTemplateColumn>   


Alphonse

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 Feb 2010, 10:53 AM
Hi Alphonse,

Try the following approach by checking number of decimal places in the "onkeyup" event, and then reset the text according to the requirement.

aspx:
 
<telerik:RadNumericTextBox ID="txtDbAmt" onkeyup="check(this);" Text='<%# bind("Number") %>' 
    MinValue="0" MaxValue="99999999" MaxLength="11" DataType="System.Decimal" Culture="en-GB" 
    AutoPostBack="false" ClientEvents-OnKeyPress="KeyPressed" EnabledStyle-HorizontalAlign="Right" 
    runat="server"
    <NumberFormat AllowRounding="false" DecimalDigits="2" DecimalSeparator="." GroupSeparator="," 
        GroupSizes="3" KeepNotRoundedValue="true" /> 
</telerik:RadNumericTextBox> 

javascript:
 
    function check(textbox) { 
        var val = textbox.value; 
        if (val.length - val.indexOf('.') > 3 && val.indexOf('.') > 0) { 
            textbox.value = val.substr(0, (val.length - 1)); 
        } 
    } 

-Shinu.
0
alphonse joseph
Top achievements
Rank 1
answered on 17 Feb 2010, 05:43 AM
Hi Shinu,
    Thanks for your quick reply.  It works under normal conditions.  But if the user keeps a particular key pressed for a while, it fails.  Is there any way to avoid this situation? 

Alphonse
0
Dimo
Telerik team
answered on 17 Feb 2010, 01:33 PM
Hi Alphonse,

Check this out:

<telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server">
    <ClientEvents OnKeyPress="checkDecimals" />
</telerik:RadNumericTextBox>
 
<script type="text/javascript">
 
function checkDecimals(sender, args)
{
    window.setTimeout(function(){
     
        var tbValue = sender.get_textBoxValue();
        var tbDecimalPosition = tbValue.indexOf(sender.get_numberFormat().DecimalSeparator);
        if (tbDecimalPosition != -1 && tbValue.length > tbDecimalPosition + 3)
        {
            sender._textBoxElement.value = tbValue.substr(0, tbDecimalPosition + 3);
        }
     
    }, 1);
}
 
</script>


Sincerely yours,
Dimo
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Input
Asked by
alphonse joseph
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
alphonse joseph
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or