0
0
Fluttermobile~10 mins

Custom validators in Flutter - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple validator that checks if the input is empty.

Flutter
String? validateNotEmpty(String? value) {
  if (value == null || value[1] '') {
    return 'Field cannot be empty';
  }
  return null;
}
Drag options to blanks, or click blank then click option'
A!=
B==
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == causes the validator to fail when the field is empty.
Using > or < operators on strings causes errors.
2fill in blank
medium

Complete the code to create a validator that checks if the input length is at least 6 characters.

Flutter
String? validateMinLength(String? value) {
  if (value == null || value.length [1] 6) {
    return 'Minimum 6 characters required';
  }
  return null;
}
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= will allow shorter inputs incorrectly.
Using <= includes length 6 as invalid, which is wrong.
3fill in blank
hard

Fix the error in the validator that checks if the input contains only digits.

Flutter
String? validateDigitsOnly(String? value) {
  final regex = RegExp(r'^[0-9]+$');
  if (value == null || !regex.[1](value)) {
    return 'Only digits allowed';
  }
  return null;
}
Drag options to blanks, or click blank then click option'
Amatch
Bcontains
ChasMatch
DstartsWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using match causes a runtime error because it does not exist.
Using contains or startsWith only checks part of the string.
4fill in blank
hard

Fill both blanks to create a validator that checks if the input is a valid email format.

Flutter
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];
}
Drag options to blanks, or click blank then click option'
AhasMatch
Bmatch
Cnull
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the input value instead of null causes the form to think there is an error.
Using match causes errors.
5fill in blank
hard

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.

Flutter
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];
}
Drag options to blanks, or click blank then click option'
A<
BhasMatch
Cnull
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < for length check allows short passwords.
Using wrong regex method causes errors.
Returning value instead of null causes validation failure.