0
0
Fluttermobile~10 mins

Form validation rules 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 TextFormField with a validator that checks if the input is empty.

Flutter
TextFormField(
  validator: (value) {
    if (value == null || value[1] '') {
      return 'Please enter some text';
    }
    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 incorrectly.
Using comparison operators like '>' or '<' on strings causes errors.
2fill in blank
medium

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

Flutter
TextFormField(
  validator: (value) {
    if (value == null || value.length [1] 6) {
      return 'Enter at least 6 characters';
    }
    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 accept short inputs.
Using '==' only checks for exact length 6, not less.
3fill in blank
hard

Fix the error in the validator to correctly check if the input contains '@' for an email field.

Flutter
TextFormField(
  validator: (value) {
    if (value == null || !value[1]('@')) {
      return 'Enter a valid email';
    }
    return null;
  },
)
Drag options to blanks, or click blank then click option'
Acontains
BindexOf
Chas
Dincludes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'indexOf' without comparing to -1 causes errors.
Using 'has' or 'includes' are not valid Dart String methods.
4fill in blank
hard

Fill both blanks to create a validator that checks if the input is not empty and contains a dot '.' character.

Flutter
TextFormField(
  validator: (value) {
    if (value == null || value[1] '' || !value[2]('.')) {
      return 'Enter a valid input';
    }
    return null;
  },
)
Drag options to blanks, or click blank then click option'
A==
B!=
Ccontains
DstartsWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' for the empty string check causes wrong validation.
Using 'startsWith' instead of 'contains' misses dots inside the string.
5fill in blank
hard

Fill all three blanks to create a validator that checks if the input is not empty, has length greater than 5, and contains '@'.

Flutter
TextFormField(
  validator: (value) {
    if (value == null || value[1] '' || value.length [2] 5 || !value[3]('@')) {
      return 'Enter a valid email';
    }
    return null;
  },
)
Drag options to blanks, or click blank then click option'
A==
B>
Ccontains
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for length check.
Using '!=' for empty string check causes wrong validation.