Data Type Control in PowerApps: How to Restrict Text Input to Whole Numbers
As developers and app creators, we often encounter challenges that require ingenious workarounds. One such challenge in PowerApps is the data type control related to text input controls. This blog offers a solution to a frequently faced issue: how to restrict a text input control to accept whole numbers only. Dive in to learn more, and if you face any challenges, contact us for expert guidance.
Understanding Text Input Controls in PowerApps
Text input controls are versatile but can sometimes lack native functionality for specific requirements. For instance, there’s no in-built way to limit data entry to just whole numbers. Although the text input control comes with a format property that allows for text and number formats, it doesn’t cater to the finer nuances like differentiating between whole numbers and decimals.
When you set the text format to ‘Number’, users can only key in numeric characters. In a browser environment, any non-numeric character input is automatically discarded. On mobile devices, PowerApps smartly displays the numeric keypad to further ensure only numeric entries.
However, a limitation is that this format does accept decimal numbers. If the requirement is to have whole numbers only, there’s no direct property to enforce it.
Method:1
select Textinput and on right side properties pane you can see option of format which you can use to control and restrict inputs to only number.
Method:2
Configuring Text Input for Whole Numbers
To limit a text input control to only whole numbers, an effective workaround involves using a formula within the ‘OnChange’ property. This formula verifies if the entered input is a whole number. If it isn’t, the system notifies the user and resets the control.
The suggested formula is:
If( TextInput1.Text <> "" And Value(TextInput1.Text) <> RoundDown(Value(TextInput1.Text),0), Notify("Not a whole number"); Reset(TextInput1) )
Reference:If and Switch functions
When implemented, if a user happens to enter a value with decimals, the app sends an alert once the user moves away from the control. Subsequently, the value is reset to its initial state. It’s essential to understand that the reset function discards the entire decimal value, and not just the decimal portion.
Conclusion
PowerApps’ text input controls might not have a native solution for restricting entries to whole numbers, but with the method described above, you can easily achieve this. It’s all about understanding the available tools and using them smartly to address specific requirements. And remember, if you ever need further assistance on this topic or any other technical issue, don’t hesitate to contact us. We’re here to help, guiding you to find the best solutions for your app-building needs.