RadTextBox need to be restricted only for integers

1 Answer 5929 Views
General Discussions
bharat veer
Top achievements
Rank 1
bharat veer asked on 30 May 2011, 07:51 PM
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.

1 Answer, 1 is accepted

Sort by
1
Accepted
Shinu
Top achievements
Rank 2
answered on 31 May 2011, 06:14 AM
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.
bharat veer
Top achievements
Rank 1
commented on 31 May 2011, 12:29 PM

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

thanks
bharat veer
Top achievements
Rank 1
commented on 01 Jun 2011, 01:53 PM

one more thing brother,

stil character cab be copy and paste, how it can be restricted.
Shinu
Top achievements
Rank 2
commented on 01 Jun 2011, 02:43 PM

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.
NTR
Top achievements
Rank 1
commented on 20 Dec 2012, 03:45 PM

I need for decimal also for only 2 digits after decimal. Example: 12.12, 123.23, 1234.12, 12345.67 like this.
Princy
Top achievements
Rank 2
commented on 21 Dec 2012, 03:41 AM

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.
NTR
Top achievements
Rank 1
commented on 25 Dec 2012, 09:59 PM

Thank You Princy.
Rajashekhar
Top achievements
Rank 1
commented on 24 Apr 2015, 01:03 PM

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

Konstantin Dikov
Telerik team
commented on 29 Apr 2015, 08:55 AM

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.

 
balaram
Top achievements
Rank 1
commented on 30 Oct 2015, 07:53 AM

Hi shinu tnks for suggition great working......................@@@
balaram
Top achievements
Rank 1
commented on 30 Oct 2015, 07:54 AM

Hi NTR iam big fan to u  ..............!!!
devanand
Top achievements
Rank 1
commented on 17 Sep 2016, 05:41 PM

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

Prateek
Top achievements
Rank 1
commented on 16 Dec 2016, 01:08 AM

I am using the below radtextbox and the onkeyup is not working for it. Below is the code:

<td align="left" style="Width:80px; padding-bottom:10px;"><telerik:RadTextBox ID="SphereRightText" runat="server" width="50" OnKeyUp="doRightSphereValidation();" onchange="FormatRightSphere(2, true);" /></td>

function doRightSphereValidation()
    {
        if (IsRightLensItemSelected())
        {
            var textBox = $find("<%=SphereRightText.ClientID %>");
            return validation_ValidateNumericTextBox(textBox, RightSphereMinimum, RightSphereMaximum, false, TurnInvalidInputsRed());
        }
        else
        {
            return true;
        }
    }

 

Konstantin Dikov
Telerik team
commented on 20 Dec 2016, 10:15 AM

Hello Prateek,

The code snippet fires the doRightSphereValidation function as expected on my side, but this is a handler added to the element of the RadTextBox and it is not an event from the API. If you need to use the control events you should handle the  ClientEvents-OnKeyPress="functionName".

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.
Majeed
Top achievements
Rank 1
commented on 28 Jul 2018, 10:00 AM

Hi Shunu,

I am facing a problem to restrict the special character by using javascript, I have implemented the same thing as you mentioned and called the function on keypress event of RadTextBox but One of our end user has disabled/stopped the javascript from client browser and he/she has insert the special character,

kindly suggest me something what I have to do.

Thanks,

Majeed Khan

Eyup
Telerik team
commented on 02 Aug 2018, 05:36 AM

Hi Majeed,

I've already replied to this query in the following post:
https://www.telerik.com/forums/don-t-allow-user-to-write-special-characters-in-radtextbox

I suggest that we continue our discussion on the mentioned thread.

Regards,
Eyup
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Afreen
Top achievements
Rank 1
commented on 20 May 2020, 08:39 AM

Hi Princy,

 

This is very urgent , can i do this for radsearchbox input text also,

need reply really soon 
i have put a search box in which currency code is populated in the dropdown of searchbox and want to restrict user to put only number (as the amount) please help

Rumen
Telerik team
commented on 22 May 2020, 12:52 PM

Hi there, 

You can see how to attach to the key press event of the search textbox via jQuery in this forum thread: https://www.telerik.com/forums/how-to-attach-client-event-to-textbox-inside-radsearchbox#yv46_mWfuUqx4dV0uzg7aA.

<script type="text/javascript">
 
           function pageLoad() {
               var $ = $telerik.$;
               $(".rsbInput ").keypress(function (event) {
                   if (event.which == 13) {
                       alert("enter pressed");
                   }
               });
           }
 
       </script>

Regards,
Rumen
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
General Discussions
Asked by
bharat veer
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or