gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
add regex in flutter input controller that numbers start with 01
- User
add regex in flutter input controller that numbers start with 01
- Assistant
To add a regex pattern in a Flutter
TextEditingControllerthat only allows numbers starting with "01", you can use the following code:import 'package:flutter/services.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: TextField( controller: TextEditingController()..text = '', inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'^01\d*')), ], keyboardType: TextInputType.number, ), ), ), ); } }In this code:
-
RegExp(r'^01\d*'): The regex pattern^01\d*is used to allow numbers starting with "01" followed by any digits. -
FilteringTextInputFormatter.allow(RegExp(r'^01\d*')): This formatter is added to theTextField'sinputFormattersproperty to restrict the input to match the specified pattern.
Now, the
TextFieldwill only allow input that starts with "01" and can be followed by any digits. -