48 lines
1.7 KiB
Dart
48 lines
1.7 KiB
Dart
class Validators {
|
|
Validators._();
|
|
|
|
static String? required(String? value, {String fieldName = 'This field'}) {
|
|
if (value == null || value.trim().isEmpty) {
|
|
return '$fieldName is required';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
static String? email(String? value) {
|
|
if (value == null || value.trim().isEmpty) return 'Email is required';
|
|
final regex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
|
|
if (!regex.hasMatch(value.trim())) return 'Enter a valid email address';
|
|
return null;
|
|
}
|
|
|
|
static String? phone(String? value) {
|
|
if (value == null || value.trim().isEmpty) return 'Phone is required';
|
|
final regex = RegExp(r'^[6-9]\d{9}$');
|
|
if (!regex.hasMatch(value.trim())) return 'Enter a valid 10-digit mobile number';
|
|
return null;
|
|
}
|
|
|
|
static String? password(String? value) {
|
|
if (value == null || value.isEmpty) return 'Password is required';
|
|
if (value.length < 8) return 'Password must be at least 8 characters';
|
|
if (!RegExp(r'[A-Z]').hasMatch(value)) return 'Must contain an uppercase letter';
|
|
if (!RegExp(r'[a-z]').hasMatch(value)) return 'Must contain a lowercase letter';
|
|
if (!RegExp(r'[0-9]').hasMatch(value)) return 'Must contain a number';
|
|
return null;
|
|
}
|
|
|
|
static String? gstNumber(String? value) {
|
|
if (value == null || value.trim().isEmpty) return null;
|
|
final regex = RegExp(r'^\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}Z[A-Z\d]{1}$');
|
|
if (!regex.hasMatch(value.trim().toUpperCase())) return 'Enter a valid GST number';
|
|
return null;
|
|
}
|
|
|
|
static String? minLength(String? value, int min, {String fieldName = 'Field'}) {
|
|
if (value == null || value.length < min) {
|
|
return '$fieldName must be at least $min characters';
|
|
}
|
|
return null;
|
|
}
|
|
}
|