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
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 />
Thanks for your quick reply and code example. It worked first time!
Kind regards
Mark Eaton
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
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.
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.
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.
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?
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.
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.
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.
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?
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.
MaxValue="999999999"
ID="RadNumericTextBox1"
runat="server">
<NumberFormat GroupSeparator="" DecimalDigits="0" />
</telerik:RadNumericTextBox>
i use this and i can still type in negative values :(
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.
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,
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
It's 2016, Is the functionality added to the NumericTextBox control ?
We had 4 major release since last time some one ask for it.
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
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
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