RadNumericTextBox to accept only postive integers

3 Answers 4436 Views
Input
Mark
Top achievements
Rank 1
Mark asked on 10 Mar 2008, 04:37 AM

Hi

Is there a way for the RadNumericTextBox to accept only postive integers?

I.e:

  • No negatives
  • No decimals
  • No group formatting

I don't know if this is the right approach but for the decimal separator I used "©".

The database stores a 10 digit positive integer.


Kind regards

Mark Eaton

3 Answers, 1 is accepted

Sort by
0
plamen
Top achievements
Rank 1
answered on 10 Mar 2008, 08:21 AM
hi:)

You can use MinValue property

Here is an example:

            <telerik:RadNumericTextBox   
                MinValue="0"   
                MaxValue="999999999"   
                ID="RadNumericTextBox1"   
                runat="server">  
                <NumberFormat GroupSeparator="" DecimalDigits="0" /> 
            </telerik:RadNumericTextBox> 


Regards...
<John:Peel />
Mark
Top achievements
Rank 1
commented on 10 Mar 2008, 10:29 PM

Hi John

Thanks for your quick reply and code example. It worked first time!


Kind regards

Mark Eaton
0
Mark DeMichele
Top achievements
Rank 1
answered on 20 Nov 2008, 03:49 PM
Hi there,

This solution works almost perfectly, but the user is still permitted to type in a decimal point. Is there any way to reject the decimal point altogether, i.e., have the same effect as if the user typed in a letter?

I can achieve something close to what I need by using the following, but in my case it doesn't make sense to allow the decimal point at all.

     <telerik:RadNumericTextBox    
                MinValue="0"    
                MaxValue="999999999"    
                ID="RadNumericTextBox1"     
                runat="server">   
                <NumberFormat GroupSeparator="" DecimalDigits="0" AllowRounding="true"   KeepNotRoundedValue="false"  />  
                 
            </telerik:RadNumericTextBox>  


Mark
0
Missing User
answered on 20 Nov 2008, 04:27 PM
Hi Mark,

The following code example demonstrates how to reject the decimal separator:

    <div> 
        <script type="text/jscript"
            function KeyPress(sender, args) { 
                if (args.get_keyCharacter() == sender.get_numberFormat().DecimalSeparator) { 
                    args.set_cancel(true); 
                } 
            } 
        </script> 
        <telerik:RadNumericTextBox     
            MinValue="0"     
            MaxValue="999999999"     
            ID="RadNumericTextBox1"      
            runat="server">    
            <NumberFormat GroupSeparator="" DecimalDigits="0" AllowRounding="true"   KeepNotRoundedValue="false"  />   
            <ClientEvents OnKeyPress="KeyPress" />                  
        </telerik:RadNumericTextBox>   
    </div> 



Greetings,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Mark DeMichele
Top achievements
Rank 1
commented on 20 Nov 2008, 05:03 PM

Thanks for the quick reply and work-around. 

That being said, I would just like to give you my oppinion on something.  In a lot of cases, the RadControls save me a lot of work so I can't really complain.  You have a good product and I appreciate your hard work and good support.  Your technical support response time is faster than any other vendor I know.

However, sometimes for such simple features you have to jump through hoops to get it to do trival things, this feature in particular, in my oppinion should have been built in in the first place.  I have a large site with many pages and many input fields.  It would have been really nice to not have to add the same javascript snippet on each and every page.  Plus, in cases where I actually need the KeyPress for something else, it really complicates things.

At some point, I think you need to compile a list of "features" that could easily be automated and implement those.  Giving the programmer and simple "drop-in" control (with no extra codiing) should be the highest priority.  A simple property that told the box it was an integer would have been perfect.  Maybe you can consider it for a future release.

Thanks again for your product.  I think it's great and you guys are doing a great job.
Missing User
commented on 21 Nov 2008, 10:26 AM

Hi Mark,


Thank you for the nice words - we are happy to hear that you like our products

We are always open for new ideas and suggestions and our new features are based on customer feedback. Keep sharing your suggestions, as it will not remain overlooked. Our main goal is to continue improving our products and make them more attractive to the clients.

Sincerely yours,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Shark75
Top achievements
Rank 2
commented on 12 Mar 2009, 03:10 PM

We have receieved a bug report with this exact problem where the user is allowed to type decimal points and minus symbols, whilst submitting the value is rejected by the range validation (MinValue, MaxValue).

Sorry, I haven't tried the fix suggested here, but I don't see that working for copying/pasting a value with decimal points or minus symbols in as it only handles key presses (copy and paste can be achieved with the mouse alone).

Is there a solution to both scenarios?

Pavel
Telerik team
commented on 16 Mar 2009, 08:13 AM

Hi Simon,

The suggested approach should work for you when you set the MinValue and DecimalDigits properties as shown. When pasting into the control, it is parsing the number according to these properties and thus will not display a negative value or a decimal separator at all.

Sincerely yours,
Pavel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Shark75
Top achievements
Rank 2
commented on 17 Mar 2009, 09:55 AM

It doesn't remove the negative symbol or decimal separator when you copy a negative number with a decimal place in it from another application and paste into the RadNumericTextBox using the content menu paste function (mouse only)...

1) Open notepad and type in -0.1.
2) Copy -0.1 into your clipboard using CTRL+C or whatever method you normally use.
3) Right click on the RadNumerictextBox and select the Paste option.
4) Because this isn't happening from a key press it accepts it, flashes the invalid styling at you, but leaves it there still.

P.S. I'm using ASP.Net Rad Controls myself (the non-ajax version), so I had to change the keypress function to check args.KeyCode and a hard coded value as you can't get at the number format stuff client side in the non-ajax version, but I've tried both it and the new Ajax version and they both suffer the same problem that this is only reacting to key presses not mouse events.
Pavel
Telerik team
commented on 18 Mar 2009, 07:39 AM

Hello Simon,

This scenario should be handled by the control itself regardless whether the custom javascript function is applied. The value will be changed when the control loses focus. Note that I just tested this with the classic version of the control as well and it behaves the same way. I am attaching a video for illustration. Take a look at it and let me know if I am missing something.

Best wishes,
Pavel
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Shark75
Top achievements
Rank 2
commented on 18 Mar 2009, 10:00 AM

Hi Pavel,

Thanks for the video.

Yes you're correct the RadNumericTextBox when it has Min and Max values on it does reset the value when you lose focus (dunno what I've done differently now to get the same behaviour as you, user error! :-)). Wierdly though in my sample application the ASP.NET AJAX version (Prometheus) doesn't reset the value, it leaves the invalid value in. What am I missing?

Sample video

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"
    <title></title
    <script type="text/javascript"
        function KeyPress(sender, args) 
        { 
            if (args.KeyCode == 45 || args.KeyCode == 46)  
            { 
                return false; 
            } 
        } 
        function NewKeyPress(sender, args)  
        { 
            var keyCharacter = args.get_keyCharacter(); 
             
            if (keyCharacter == sender.get_numberFormat().DecimalSeparator || 
                keyCharacter == sender.get_numberFormat().NegativeSign)  
            { 
                args.set_cancel(true); 
            } 
        }  
    </script>  
</head> 
<body> 
    <form id="form1" runat="server"
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server"
    </telerik:RadScriptManager> 
    <div> 
        <span>RadNumericTextBox:</span> 
        <rad:RadNumericTextBox ID="RadNumericTextBox1" runat="server"    
            MinValue="1" MaxValue="999999999" >     
            <NumberFormat GroupSeparator="" DecimalDigits="0" AllowRounding="true" KeepNotRoundedValue="false"  />    
            <ClientEvents OnKeyPress="KeyPress" />                   
        </rad:RadNumericTextBox>    
        <br /><br /> 
        <span>ASP.NET AJAX RadNumericTextBox:</span> 
        <telerik:RadNumericTextBox ID="RadNumericTextBox2" runat="server" 
            MinValue="1" MaxValue="999999999" >     
            <NumberFormat GroupSeparator="" DecimalDigits="0" AllowRounding="true" KeepNotRoundedValue="false"  />    
            <ClientEvents OnKeyPress="NewKeyPress" />                   
        </telerik:RadNumericTextBox>    
    </div> 
    </form> 
</body> 
</html> 
 

P.S. What do you use to capture the video, I've been using CamStudio, which is ok, but a bit clumsy?
Pavel
Telerik team
commented on 19 Mar 2009, 08:32 AM

Hello Simon,

The problem when using this markup is caused by a bug in the Asp.Net AJAX version of the control. When used on the same page with the classic version of RadInput the control seems to be using the wrong scripts, so it is generating javascript errors. I have notified our developers about this and they will look into it. I have also updated your telerik points for bringing it to our attention. Note that if you use only the new controls you should not experience this problem.

As for the video recording, I use SnagIt which is easy to use and pretty much covers everything you might need.

I hope this helps.

Best wishes,
Pavel
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Mateusz
Top achievements
Rank 1
commented on 06 Dec 2012, 10:08 AM

MinValue="0"
MaxValue="999999999"
ID="RadNumericTextBox1"
runat="server">
<NumberFormat GroupSeparator="" DecimalDigits="0" />
</telerik:RadNumericTextBox>

i use this and i can still type in negative values :(
Princy
Top achievements
Rank 2
commented on 06 Dec 2012, 11:16 AM

Hi,

Try the following code snippet to cancel the negative values in the RadNumericTextBox.

ASPX:
<telerik:RadNumericTextBox MinValue="0" MaxValue="999999999" ID="RadNumericTextBox1"
    runat="server">
    <NumberFormat GroupSeparator="" DecimalDigits="0" />
    <ClientEvents OnKeyPress="NewKeyPress" />
</telerik:RadNumericTextBox>

JS:
<script type="text/javascript">
    function NewKeyPress(sender, args) {
        var keyCharacter = args.get_keyCharacter();
        if (keyCharacter == sender.get_numberFormat().NegativeSign) {
            args.set_cancel(true);
        }
    
</script>

Hope this helps.

Regards,
Princy.
Jean-Christian
Top achievements
Rank 1
commented on 10 Nov 2015, 03:49 PM

Hi,

I have the same problem as Mark. Indeed, user can type the decimal separator even if the decimaldigits is set to 0.

in 2008, you proposed to insert a little javascript code in the page. But, as for Mark, it is difficult for me to insert a lot of little javascript code blocs everywhere in my pages. Moreover, if I need to change the backcolor of a radnumerictextbox, Telerik poroposes to add a little javascript, if I want to hide a radcalandar when the focus goes to another control, Telerik proposes to insert a little javascript, and so on... At the end, I need to add and maintain a lot of little javascript code blocs in my pages and it becomes really boring.

In 2015, is there a way to prevent the user from typing a decimal separator without having to add a "little javascript".

Regards,

 

Maria Ilieva
Telerik team
commented on 13 Nov 2015, 10:40 AM

Hi Jean-Christian,

Unfortunately the required functionality is still not added to the NumericTextBox control and you should add custom client code to achieve the required functionality.


Regards,
Maria Ilieva
Telerik
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 Feedback Portal and vote to affect the priority of the items
Pierre
Top achievements
Rank 1
commented on 27 Sep 2016, 02:56 PM

Hi,

It's 2016, Is the functionality  added to the NumericTextBox control ?
We had 4 major release since last time  some one ask for it.

 
Maria Ilieva
Telerik team
commented on 30 Sep 2016, 11:36 AM

Hi Pierre,

At the moment, the required functionality can only be achieved by using custom client code for canceling the negative values.

Excuse us for any inconvenience this issue may lead.


Regards,
Maria Ilieva
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Lee
Top achievements
Rank 1
commented on 10 Dec 2019, 06:42 PM

Hi 

It's now near the end of 2019, is the functionality added to avoid the user being able to type non-digit values in (i.e decimal points)

 

I completely agree with the other posters, on balance I've saved a lot of time but there's quite a few features missing "out of the box" that require javascript hacks which I would rather not do - this flies in the face of maintainability of code 

 

Attila Antal
Telerik team
commented on 13 Dec 2019, 12:33 PM

Hi Lee,

RadNumericTextBox by default does not allow introducing non-digit values (non-numeric).

<telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server"></telerik:RadNumericTextBox>

 

When typing non-numeric values, the textbox shows a warning like the following figure:

However, both positive and negative values are considered numeric which is allowed. 

If you would like to change that to allow only positive numbers, you can take advantage of the MinValue property which does not require any JavaScript hack to be applied. If you set it to be 0, then no numeric value below 0 will be allowed to be introduced:

<telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server" MinValue="0"></telerik:RadNumericTextBox>

 

Here is a Demo

 

Kind regards,
Attila Antal
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.
Tags
Input
Asked by
Mark
Top achievements
Rank 1
Answers by
plamen
Top achievements
Rank 1
Mark DeMichele
Top achievements
Rank 1
Missing User
Share this question
or