string_validify 1.0.0
string_validify: ^1.0.0 copied to clipboard
A lightweight, zero-dependency Dart package for string validation, formatting, and common input checks. Includes email, phone, URL, credit card, password validators and string utilities.
String Validify #
A lightweight, zero-dependency Dart package for string validation, formatting, and common input checks. Perfect for form validation in Flutter apps or any Dart project.
Features #
- Email Validation - Validate, extract domain/username, mask for privacy
- Phone Validation - Validate international numbers, sanitize, mask
- URL Validation - Validate with/without scheme, extract domain, auto-add scheme
- Credit Card Validation - Luhn algorithm check, card type detection, masking, formatting
- Password Validation - Configurable rules, strength scoring
- String Utilities - Capitalize, titleCase, camelCase, snakeCase, truncate, mask, initials, and more
- Common Validators - Required, minLength, maxLength, pattern matching, range checks
Installation #
Add string_validify to your pubspec.yaml:
dependencies:
string_validify: ^1.0.0
Then run:
dart pub get
Usage #
Email Validation #
import 'package:string_validify/string_validify.dart';
EmailValidator.isValid('user@example.com'); // true
EmailValidator.validate('bad-email'); // 'Invalid email address'
EmailValidator.getDomain('user@gmail.com'); // 'gmail.com'
EmailValidator.mask('john.doe@gmail.com'); // 'j******e@gmail.com'
Phone Validation #
PhoneValidator.isValid('+1234567890'); // true
PhoneValidator.sanitize('+1 (234) 567-890'); // '+1234567890'
PhoneValidator.mask('+1234567890'); // '+12****7890'
URL Validation #
UrlValidator.isValid('https://example.com'); // true
UrlValidator.isValidLoose('example.com'); // true (no scheme required)
UrlValidator.ensureScheme('example.com'); // 'https://example.com'
UrlValidator.getDomain('https://example.com/path');// 'example.com'
Credit Card Validation #
CreditCardValidator.isValid('4111111111111111'); // true (Luhn check)
CreditCardValidator.getCardType('4111...'); // CardType.visa
CreditCardValidator.mask('4111111111111111'); // '**** **** **** 1111'
CreditCardValidator.format('4111111111111111'); // '4111 1111 1111 1111'
Password Validation #
PasswordValidator.isValid('Abcdefg1'); // true
PasswordValidator.getStrength('Abcdefghij12345!').label; // 'Very Strong'
PasswordValidator.validate('weak'); // 'Password must be at least 8 characters'
// Custom rules
PasswordValidator.validate('MyPass1',
minLength: 10,
requireSpecialChar: true,
);
String Utilities #
StringUtils.capitalize('hello world'); // 'Hello world'
StringUtils.titleCase('hello world'); // 'Hello World'
StringUtils.camelCase('hello world'); // 'helloWorld'
StringUtils.snakeCase('helloWorld'); // 'hello_world'
StringUtils.truncate('Hello world', 5); // 'Hello...'
StringUtils.mask('SensitiveData'); // 'Se*********ta'
StringUtils.initials('John Doe'); // 'JD'
StringUtils.wordCount('Hello world'); // 2
StringUtils.isNumeric('12345'); // true
StringUtils.isAlpha('hello'); // true
Common Validators #
CommonValidator.required(''); // 'This field is required'
CommonValidator.minLength('hi', 5); // 'Must be at least 5 characters'
CommonValidator.maxLength('hello world', 5); // 'Must be at most 5 characters'
CommonValidator.matches('abc', 'xyz'); // 'Values do not match'
CommonValidator.numberInRange('5', min: 1, max: 10); // null (valid)
Flutter Form Integration #
All validate() methods return String?, making them perfect for Flutter's TextFormField:
TextFormField(
validator: (value) => EmailValidator.validate(value),
);
TextFormField(
validator: (value) => PasswordValidator.validate(value,
minLength: 10,
requireSpecialChar: true,
),
);
TextFormField(
validator: (value) => CommonValidator.required(value, fieldName: 'Name'),
);
License #
MIT License - see LICENSE for details.