Flutter Regex: Make data validation more simple with flutter_regex

With over 1000 regex patterns, you can efficiently validate user inputs and data formats

Shirsh Shukla
6 min readFeb 26, 2024

In this article, we will discuss Flutter regular expressions (regex) and the flutter_regex plugin. The plugin supports over 1000 regex patterns, making it incredibly useful. From basic data validation (like usernames, emails, and passwords) to specialized patterns for different data formats and over 50 regex patterns for country IDs, these patterns cover a wide range of applications.

But first, let’s see what exactly is a regex.

It’s a string of text that helps you create patterns to find and manage text. While regular expressions are commonly used in programming languages like Perl, they can also be used in other places, like the command line and text editors, to search for text in files.

Regex might seem challenging at first, but once you master it, you can save a lot of time, especially when dealing with large text files or data sets.

In Flutter development, regex is used to validate usernames, emails, passwords, and phone numbers. It is common for developers to create separate Util classes to store regex patterns. However, finding and implementing these patterns can be challenging, especially when it comes to emoji regexes or UUIDs.

The flutter_regex plugin comes in handy here. A number of features are available, including emoji patterns, 50+ country ID regex patterns, and patterns for usernames from Google, Instagram, and Discord. It simplifies complex data format validation, such as UUIDs, Bitcoin addresses, Ethereum addresses, and stock exchange codes.

So in this article, we see some of the most useful regex, like

1. User name, email, phone number

2. Password( simple, normal, hard)

3. Emoji

4. Social ID(Google, Twitter, YouTube, Discord etc.)

5. Currencies(dolor 💵,Rupees ₹, euro 💶) and Crypto Currencies(Bitcoin, Ethereum)

6. International IDs(ISIN, IBAN,IMEI, ISMN etc.)

7. URL, URI, UUID

8. Date 📅 Time ⏲ with different formats.

9. Popular documents types(doc, excel, audio, video, txt, ppt, ipa, apk, pdf etc.)

10. Security keys (sha 1, sha 256, ssn, ipv4, ipv6, etc.)

11. Colors (RGB, HSL, etc.)

12. JSON(JSON object, JSON array, JWT)

13. Postal code(USA, Indian, Japanese, German, French etc.)

14. Governments IDs(License, Passport, voters ID and 10 plus)

For (USA, UK, China, India, Germany, France, Pakistan, Bangladesh, Japan etc, for 50 plus countries)

15. Credit card (15 &16 digits both also space supports) 💳

And almost 1000+ regex we get by flutter_regex package.

With flutter_regex, developers can easily validate various data formats, making it a valuable tool for Flutter developers looking to streamline their data validation processes.

So let’s see how it is useful for us by some of the examples,

To use the flutter_regex plugin,it start with to add it to your pubspec.yaml file:

dependencies:
flutter_regex: ^0.0.3

Then, import the package in your Dart code:

import 'package:flutter_regex/flutter_regex.dart';

Now you are ready to use this plugin, let’s see this with the most used 15 examples,

1. Username, Email, Phone Number, or all normal form fields

String email = 'abc@gmail.com';
String phoneNumber = '+91 9999999999';

bool isEmailMatch = email.isEmail();
bool isPhoneNumberMatch = phoneNumber.isPhone();

also allow top-level domain

String emailSt = 'user@sub.exämple.com';
bool isStEmailMatch = emailSt.isEmail(supportTopLevelDomain: true); //true

2. Password (Simple, Normal, Hard)

// Simple
String easyPassword = 'abcdefgh';
bool isPasswordMatch = easyPassword.isPasswordEasy();
// Normal
String normalPassword = 'admin123';
bool isPasswordMatch = normalPassword.isPasswordNormal1();
// Hard
String hardPassword = 'Admin123!';
bool isPasswordMatch = hardPassword.isPasswordHard();

Also, there are 2–3 more regex for the all-type password and space
support option as well.

3. Emoji (😂)

String emoji = '😂';
bool isEmojiMatch = emoji.isEmoji();
All common emojis supported like, 😂 ❤️ 😍 😊 😭 😘 🥺 🤔 💕 😩 ✨ 🎉 🙏 😊 🎶 😁 😔 😎 😬 🤣 🌟 😢 💔 🥰 🌹 💖 🙌 🤗 💙 😅 🎊 🎁 😌 😪 😅 😳 😤 🥴 🙃 🎵 😓 😖 😞 😇 😜 😋 😡

4. Social ID(Google, Twitter, YouTube, Discord etc.)

String usernameGoogle = 'Shirsh123';
bool isUsernameGoogleMatch = usernameGoogle.isUsernameGoogle(); // same for Instagram, Discord etc.

5. Currencies(dolor 💵,Rupees ₹, euro 💶 and 110 + country) and Crypto Currencies(Bitcoin, Ethereum)

String currency = '€'; // Support Euro, dollar, rupee, and 110+ country
bool isCurrencyMatch = currency.isCurrencySymbolRegex();

also allow Currency Code Pattern

String currencyCode = 'EUR';
bool isCurrencyCodeMatch = currencyCode.isCurrencyCodePattern(); //true

6. International IDs(ISIN, IBAN,IMEI, ISMN etc.)

// ISIN (International Securities Identification Number)
String isin = 'INE0J1Y01017';
bool isISINMatch = isin.ISIN();
//  International Standard Book Number (ISBN) 
String isbn = '978-93-5300-895-6';
bool isISBNMatch = isbn.ISBN();

Same as we can check,
IMEI (International Mobile Equipment Identity) number and
ISMN (International Standard Music Number) etc.

7. URL, URI, UUID

String url = 'http://www.example.com/index.html';
bool isUrlMatch = url.isUrl();
String uri = 'https://google.com';
bool isUrlMatch = uri.isUri();
String uuid = '550e8400-e29b-41d4-a716-446655440000';
bool isUrlMatch = uuid.isUUID();

8. Date 📅 Time ⏲ with different formats.

String dateTime = 'Wednesday, 24 June 2023';
bool isDateTimeMatch = dateTime.isDateTime();
String dateTimeUTC = '2021-04-27 03:16:39';
bool isDateTimeUTCMatch = dateTimeUTC.isDateTimeUTC();

// Also same as other.

9. Popular documents types(doc, excel, audio, video, txt, ppt, ipa, apk, pdf etc.)

String ppt = 'abc123Slide.ppt';
bool isPPTMatch = ppt.isPPT();
// same as for doc, excel, audio, video, txt, ipa, apk, pdf etc.

10. Security keys (sha 1, sha 256, ssn, ipv4, ipv6, etc.)

String sha1 = '5e13ae4640ae4ae0e09c05b7bb060f544dabd042';
bool isSHA1Match = sha1.isSHA1();
// same as for sha 256, ssn, ipv4, ipv6, etc.

11. Colors (RGB, HSL, etc.)

String rgbColor = 'rgb(256, 2, 0)';
bool isRgbColorMatch = rgbColor.isRgbColorCode();
String hslColor = 'hsl(0, 100%, 50%)';
bool ishslColorMatch = hslColor.isHslColorCode();

12. JSON(JSON object, JSON array, JWT)

// JSON object
String jsonObject = '{"name": "John", "age": 30}';
bool isJsonObjectMatch = jsonObject.isJsonObject();
// JSON Array
String jsonArray = '["apple", "banana", "orange"]';
bool isjsonArrayMatch = jsonArray.isJsonArray();
// JSON Web Token
String jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
bool isJWTMatch = jwt.isJwt();

13. Postal code(USA, Indian, Japanese, German, French etc.)

String indianPostalCode = '452006';
bool isIndianPostalCodeMatch = indianPostalCode.isIndianPostalCode();
// same as for Japanese, German, French etc

14. Governments IDs(License, Passport, voters ID and 10 plus)

String passportNumberIndia = 'AB12345'; // Just a example of Passport Number
bool isPassportNumberIndiaMatch = passportNumberIndia.isPassportNumberIndia();

// Same as India, United States, China, Japan, Germany, United Kingdom,
// France, Italy, Canada, South Korea, Russia, Spain, Australia, Brazil,
// Mexico, Indonesia, Netherlands, Saudi Arabia, Turkey, Switzerland, Taiwan,
// Sweden, Poland, Belgium, Thailand, Iran, Austria, Norway,
// United Arab Emirates, Nigeria, Israel, South Africa, Hong Kong,
// Denmark, Singapore, Malaysia, Philippines, Colombia, Egypt, Finland,
// Chile, Ireland, Pakistan, Greece, Portugal, Vietnam
// 50 plus country's top 10 ID supported

15. Credit card (15 &16 digits both also space supports) 💳

String passportNumber = '4111 1111 1111 1111'; // 16 and 15 digit also space supported
bool isPassportNumberMatch = passportNumber.isCreditCard();

In conclusion, the flutter_regex plugin simplifies data validation tasks in Flutter development thanks to its comprehensive set of regex patterns. Over 1000 regex patterns, including emojis, country IDs, usernames, and various data formats, are supported by the plugin, simplifying the process of validating user inputs.

With flutter_regex in your Flutter projects, your app can handle user input accurately and efficiently, improving user experience.

In addition, I gathered this information by researching multiple websites. Please let me know if you come across any incorrect or misinterpreted information by leaving a comment.

If you got something wrong? Mention it in the comments. I would love to improve. your support means a lot to me! If you enjoy the content, I’d be grateful if you could consider subscribing to my YouTube channel as well.

I am Shirsh Shukla, a creative Developer, and a Technology lover. You can find me on LinkedIn or maybe follow me on Twitter or just walk over my portfolio for more details. And of course, you can follow me on GitHub as well.

Have a nice day!🙂

--

--

Shirsh Shukla

SDE at Reliance Jio | Mobile Application Developer | Speaker | Technical Writer | community member at Stack Overflow | Organizer @FlutterIndore