Controlling type of number on text field

I would like to control the type of number I put on text field. My requirement is that user can enter only non zero and positive whole number. No decimals and 0 allowed. How do I do that?

This is unfortunately trickier than it should be in Axure…

First of all, you’re better off keeping your Text Field widget with the default Type:Text and not Type:Number (as that will cause the browser to pop in a spinner control, allowing user to increment/decrement the value without triggering an OnTextChange event, and allow user to spin it right down past 0 to negative numbers. It will also allow decimal input and treat ‘.’ (period character) as valid “numeric input”.

So, use the default Text Input widget and then add a few conditions to restrict what kind of characters you’ll allow. Here is some code for your widget which should work for your requirements:

Case 1 handles if user enters “0” and bumps to the minimum value of 1 --I would recommend showing an error, like setting the input field to selected (with say, a red border) and/or text message appearing with “Sorry, values must be greater than zero.”)

Case 2 handles anything that is not numeric and just clears the field. (Custom error message would help again.)

Case 3 handles decimals by stripping that pesky period char out of the input string. Here is the “Set text to: value” code:

You can also set the Max Length for the Text Field to limit the number of digits, For example, ‘2’ would allow only 1–99 as input.

1 Like