πŸš€ DevOps Certified Professional
πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 | 🀝 +1 (469) 756-6329 πŸ” Contact@DevOpsSchool.com

How to create a number input field in Flutter? Only 10 digital

DevOps

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

To limit the number of digits entered in a TextField widget to 10 digits, you can use the inputFormatters property of the TextField widget.

TextEditingController _controller = TextEditingController();
final tenDigitsOnly = new RegExp(r'^\d{0,10}$');

TextField(
  controller: _controller,
  keyboardType: TextInputType.number,
  inputFormatters: [
    FilteringTextInputFormatter.allow(tenDigitsOnly)
  ],
)

In this code, we have created a regular expression tenDigitsOnly that matches only digits and has a maximum length of 10. Then we have passed it to the FilteringTextInputFormatter.allow() method which allows only characters that match the provided regular expression.

By using this code, the user will only be able to enter up to 10 digits in the TextField.

Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x