flutter_validators 1.1.0
flutter_validators: ^1.1.0 copied to clipboard
A pure dart package of String validators and sanitizers. Inspired by validator.js.
Flutter Validators
The most comprehensive string validation package for Dart & Flutter.
Inspired by validator.js ยท 20+ validators ยท Works with Flutter Forms out of the box
A pure Dart package with 20+ string validators and sanitizers, from emails and URLs to credit cards and UUIDs. Use them as simple functions, convenient String extensions, or plug them directly into Flutter's TextFormField with the built-in Validator class. Zero dependencies, fully tested.
๐ฆ Installation #
dependencies:
flutter_validators: ^1.1.0
Then run:
dart pub get
๐ Quick Start #
import 'package:flutter_validators/flutter_validators.dart';
Use as String Extensions #
'foo@bar.com'.isEmail; // true
'https://google.com'.isURL; // true
'4111111111111111'.isCreditCard; // true
'abc123'.isAlphanumeric; // true
Use as Top-Level Functions #
isEmail('foo@bar.com'); // true
isURL('https://google.com'); // true
isIP('192.168.1.1'); // true
๐ Flutter Form Integration #
The Validator class returns String? Function(String?) closures โ exactly what TextFormField.validator expects. Each method accepts a custom errorMessage.
Form(
child: Column(
children: [
TextFormField(
decoration: const InputDecoration(labelText: 'Email'),
validator: Validator.email(errorMessage: 'Enter a valid email'),
),
TextFormField(
decoration: const InputDecoration(labelText: 'Website'),
validator: Validator.url(),
),
TextFormField(
decoration: const InputDecoration(labelText: 'Age'),
validator: Validator.numeric(errorMessage: 'Must be a number'),
),
],
),
)
Tip: Use
Validator.required()alongside other validators to enforce non-empty fields.
See the example/ directory for a complete working app.
๐ All Validators #
Every validator is available both as a top-level function and as a String extension.
| Validator | Extension | Description |
|---|---|---|
isEmail(str) |
str.isEmail |
Valid email address |
isURL(str) |
str.isURL |
Valid HTTP/HTTPS URL |
isIP(str, [version]) |
str.isIP / str.isIPv4 / str.isIPv6 |
Valid IP address (v4 or v6) |
isUUID(str) |
str.isUUID |
Valid UUID |
isCreditCard(str) |
str.isCreditCard |
Credit card number (Luhn algorithm) |
isDate(str) |
str.isDate |
Parseable date string |
isJson(str) |
str.isJson |
Valid JSON |
isInt(str) |
str.isInt |
Valid integer |
isNumeric(str) |
str.isNumeric |
Valid number (int or float) |
isAlpha(str) |
str.isAlpha |
Letters only (aโz, AโZ) |
isAlphanumeric(str) |
str.isAlphanumeric |
Letters and numbers only |
isAscii(str) |
str.isAscii |
ASCII characters only |
isBase32(str) |
str.isBase32 |
Base32 encoded |
isBase58(str) |
str.isBase58 |
Base58 encoded |
isBoolean(str) |
str.isBoolean |
Boolean string (true/false/1/0) |
isHexColor(str) |
str.isHexColor |
Hex color code (#fff, ff0000) |
isPhone(str) |
str.isPhone |
Valid phone number |
isLength(str, min, [max]) |
str.isLength(min, [max]) |
Length within range |
equals(str, comparison) |
str.equals(comparison) |
Exact string match |
๐๏ธ Form Validator API Reference #
All methods on the Validator class return String? Function(String?):
Validator.required({String errorMessage})
Validator.email({String errorMessage})
Validator.url({String errorMessage})
Validator.ip({int? version, String errorMessage})
Validator.date({String errorMessage})
Validator.numeric({String errorMessage})
Validator.integer({String errorMessage})
Validator.alpha({String errorMessage})
Validator.alphanumeric({String errorMessage})
Validator.phone({String errorMessage})
Validator.creditCard({String errorMessage})
Validator.json({String errorMessage})
Validator.uuid({String errorMessage})
Validator.hexColor({String errorMessage})
Validator.ascii({String errorMessage})
Validator.base32({String errorMessage})
Validator.base58({String errorMessage})
Validator.boolean({String errorMessage})
Validator.equals(String comparison, {String errorMessage})
Validator.length(int min, {int? max, String errorMessage})
๐ค Contributing #
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
๐ License #
This project is MIT licensed.