Complete the code to add a simple validation that checks if the input is not empty.
TextFormField(
validator: (value) {
if (value == null || value[1] '') {
return 'Please enter some text';
}
return null;
},
)The validator checks if the input value is equal to an empty string to ensure the user entered some text.
Complete the code to validate that the input length is at least 5 characters.
TextFormField(
validator: (value) {
if (value != null && value.length [1] 5) {
return 'Enter at least 5 characters';
}
return null;
},
)The validator returns an error if the input length is less than 5 characters.
Fix the error in the validation that checks if the input is a valid email format.
TextFormField(
validator: (value) {
if (value == null || !RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').[1](value)) {
return 'Enter a valid email';
}
return null;
},
)The RegExp method 'hasMatch' checks if the input matches the email pattern.
Fill both blanks to create a validation that ensures the input is a number between 1 and 10.
TextFormField(
validator: (value) {
final numValue = int.tryParse(value ?? '');
if (numValue == null || numValue [1] 1 || numValue [2] 10) {
return 'Enter a number between 1 and 10';
}
return null;
},
)The validation checks if the number is less than 1 or greater than 10 to reject invalid inputs.
Fill all three blanks to create a validation that returns an error if the input is empty, less than 3 characters, or contains spaces.
TextFormField(
validator: (value) {
if (value == null || value[1] '' || value.length [2] 3 || value.contains([3])) {
return 'Invalid input';
}
return null;
},
)The validation checks if the input is empty (== ''), shorter than 3 characters (< 3), or contains a space character (' ').