RadTextBox need to be restricted only for integers

13 posts, 1 answers
  1. bharat veer
    bharat veer avatar
    41 posts
    Member since:
    Dec 2009

    Posted 30 May 2011 Link to this post

    Good day all,

    I have a radtextbox.
    how this radtextbox can be restricted to only numbers.

    means only numbers should be entered in textbox.



    thanks to all.
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 31 May 2011 Link to this post

    Hello Bharat,

    You can use RadNumericTextBox which restricts user input to numeric values. Take a look at the following help article for more on this.
    RadNumericTextBox Basics.

    In the case of RadTextBox, you can achieve this by using Regular expression. Here is a sample code to achieve the same.
    aspx:
    <telerik:RadTextBox ID="RadTextBox1" runat="server">
        <ClientEvents OnKeyPress="keyPress" />
    </telerik:RadTextBox >
    Javascript:
    function keyPress(sender, args)
       {
           var text = sender.get_value() + args.get_keyCharacter();
           if (!text.match('^[0-9]+$'))
               args.set_cancel(true);
       }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. bharat veer
    bharat veer avatar
    41 posts
    Member since:
    Dec 2009

    Posted 31 May 2011 Link to this post

    u r really ------------------gr8.

    thanks
  5. bharat veer
    bharat veer avatar
    41 posts
    Member since:
    Dec 2009

    Posted 01 Jun 2011 Link to this post

    one more thing brother,

    stil character cab be copy and paste, how it can be restricted.
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 01 Jun 2011 Link to this post

    Hello Bharat,

    Try this approach.
    aspx:
    <telerik:RadTextBox ID="RadTextBox1" runat="server" oncopy="return false" onpaste="return false" oncut="return false">
                <ClientEvents OnKeyPress="keyPress" />
    </telerik:RadTextBox>

    Thanks,
    Shinu.
  7. NTR
    NTR avatar
    11 posts
    Member since:
    Oct 2012

    Posted 20 Dec 2012 Link to this post

    I need for decimal also for only 2 digits after decimal. Example: 12.12, 123.23, 1234.12, 12345.67 like this.
  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 20 Dec 2012 Link to this post

    Hi,

    With reference to this forum thread there is no native way to prevent users from entering more than X decimal numbers. However, you can subscribe to the KeyPress client event and cancel it if a specific number of decimal places has been reached. Note that you will have to check what is the current caret position as follows.

    ASPX:
    <telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server">
        <NumberFormat DecimalDigits="2" AllowRounding="true" />
        <ClientEvents OnKeyPress="preventMoreDecimalPlaces" />
    </telerik:RadNumericTextBox>

    JS:
    <script type="text/javascript">
    function preventMoreDecimalPlaces(sender, args)
    {
        var separatorPos = sender._textBoxElement.value.indexOf(sender.get_numberFormat().DecimalSeparator);
        if (args.get_keyCharacter().match(/[0-9]/) &&
            separatorPos != -1 &&
            sender.get_caretPosition() > separatorPos + sender.get_numberFormat().DecimalDigits)
            {
                args.set_cancel(true);
            }
    }
    </script>

    Hope this helps.

    Regards,
    Princy.
  9. NTR
    NTR avatar
    11 posts
    Member since:
    Oct 2012

    Posted 25 Dec 2012 Link to this post

    Thank You Princy.
  10. Rajashekhar
    Rajashekhar avatar
    2 posts
    Member since:
    Jan 2015

    Posted 24 Apr 2015 Link to this post

    Hi,

     in my application radmaskedtextbox should allow 9999999999(##########) number, here # is represent should 9(nine) number how to write RegularExpressionValidator validation

    my application account number is  is 9999999999 so user enter only  9999999999

    ex:- 1)user enter 9999999998 show error enter only 9999999999 

    2) user enter 9999999999 radmaskedtextbox  accepting now there is no validataion,

    3) user enter 10000000000 show error enter only 9999999999 

    4)  user enter 9874561239 show error enter only 9999999999 

    is it possible or not please help me

  11. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1803 posts

    Posted 29 Apr 2015 Link to this post

    Hi Rajashekhar,

    I am not sure what could be the use case of such requirement, since the user will be allowed to enter only one value. Nevertheless, you could try to use CustomValidator in the following manner:
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function validateValue(sender, args) {
                var textBox = $find("<%=RadMaskedTextBox1.ClientID%>");
                if (textBox.get_value() != "999999") {
                    args.IsValid = false;
                }
            }
        </script>
    </telerik:RadCodeBlock>
     
    <telerik:RadMaskedTextBox runat="server" Mask="######" ID="RadMaskedTextBox1"></telerik:RadMaskedTextBox>
    <asp:CustomValidator runat="server" ID="CustomValidator1" ErrorMessage="Value must be 999999" Display="Dynamic" ClientValidationFunction="validateValue"></asp:CustomValidator>
     
    <telerik:RadButton runat="server" Text="Test"></telerik:RadButton>

    Hope this helps.

    Regards,
    Konstantin Dikov
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  12. balaram
    balaram avatar
    2 posts
    Member since:
    Aug 2015

    Posted 30 Oct 2015 in reply to Shinu Link to this post

    Hi shinu tnks for suggition great working......................@@@
  13. balaram
    balaram avatar
    2 posts
    Member since:
    Aug 2015

    Posted 30 Oct 2015 in reply to NTR Link to this post

    Hi NTR iam big fan to u  ..............!!!
  14. devanand
    devanand avatar
    1 posts
    Member since:
    Sep 2016

    Posted 17 Sep in reply to Shinu Link to this post

    hello dude its great to receive your sharing of knowledge

    i have a Rad text box at winforms where i need to filter only numbers and email validation in another text box how can i apply that code or reference to filter it

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017