use regex in flutter text filed widget to allow the field accept numbers start by 01 only
- User
use regex in flutter text filed widget to allow the field accept numbers start by 01 only
- Assistant
You can use the
inputFormattersproperty of theTextFieldwidget in Flutter to apply a regular expression to allow only numbers starting with "01". Here's an example code snippet:dartimport 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Regex TextField Example'), ), body: Center( child: TextField( inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'^01\d*')), ], decoration: InputDecoration( hintText: 'Enter number starting with 01', ), ), ), ), ); } } - User
this is not work prevent me from writing any thing
- Assistant
I apologize for the mistake in the previous example. Let me provide you with an updated example using the
inputFormattersproperty with the correct regular expression to allow numbers starting with "01":dartimport 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Regex TextField Example'), ), body: Center( child: TextField( inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'^01[0-9]*')), ], decoration: InputDecoration( hintText: 'Enter number starting with 01', ), ), ), ), ); } }