Complete the code to create a simple validator that checks if the input is empty.
String? validateNotEmpty(String? value) {
if (value == null || value[1] '') {
return 'Field cannot be empty';
}
return null;
}The validator returns an error message if the input is null or equals an empty string. The operator == checks for equality.
Complete the code to create a validator that checks if the input length is at least 6 characters.
String? validateMinLength(String? value) {
if (value == null || value.length [1] 6) {
return 'Minimum 6 characters required';
}
return null;
}The validator returns an error if the input length is less than 6. The operator < checks this condition.
Fix the error in the validator that checks if the input contains only digits.
String? validateDigitsOnly(String? value) {
final regex = RegExp(r'^[0-9]+$');
if (value == null || !regex.[1](value)) {
return 'Only digits allowed';
}
return null;
}match causes a runtime error because it does not exist.contains or startsWith only checks part of the string.The hasMatch method checks if the entire string matches the pattern. Using match or others causes errors or wrong checks.
Fill both blanks to create a validator that checks if the input is a valid email format.
String? validateEmail(String? value) {
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
if (value == null || !emailRegex.[1](value)) {
return 'Enter a valid email';
}
return [2];
}match causes errors.The hasMatch method checks if the input matches the email pattern. The validator returns null when the input is valid, meaning no error.
Fill all three blanks to create a validator that checks if the input password has at least 8 characters, contains a digit, and returns null if valid.
String? validatePassword(String? value) {
final digitRegex = RegExp(r'\d');
if (value == null || value.length [1] 8 || !digitRegex.[2](value)) {
return 'Password must be 8+ chars and include a digit';
}
return [3];
}The validator checks if the password length is less than 8 and if it does not contain a digit using hasMatch. It returns null when valid.