0
0
React Nativemobile~20 mins

Form validation patterns in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when submitting a form with empty required fields?

Consider a React Native form with required text inputs and a submit button. What is the expected behavior when the user taps submit without filling any required fields?

React Native
import React, { useState } from 'react';
import { View, TextInput, Button, Text } from 'react-native';

export default function MyForm() {
  const [name, setName] = useState('');
  const [error, setError] = useState('');

  const handleSubmit = () => {
    if (!name) {
      setError('Name is required');
    } else {
      setError('');
      // proceed with submission
    }
  };

  return (
    <View>
      <TextInput
        placeholder="Name"
        value={name}
        onChangeText={setName}
        accessibilityLabel="Name input"
      />
      {error ? <Text accessibilityLiveRegion="polite" style={{color: 'red'}}>{error}</Text> : null}
      <Button title="Submit" onPress={handleSubmit} accessibilityLabel="Submit form" />
    </View>
  );
}
AThe form shows an error message below the input and does not submit.
BThe form submits successfully even if the input is empty.
CThe app crashes due to missing input validation.
DThe input field automatically fills with a default value.
Attempts:
2 left
💡 Hint

Think about what the code does when the input is empty.

📝 Syntax
intermediate
2:00remaining
Which option correctly validates an email format in React Native?

Choose the code snippet that correctly validates an email string using a regular expression in React Native.

React Native
const validateEmail = (email) => {
  const regex = /^[\w-.]+@[\w-]+\.[a-z]{2,}$/i;
  return regex.test(email);
};
Aconst validateEmail = email => email.test(/^[\w-.]+@[\w-]+\.[a-z]{2,}$/i);
Bconst validateEmail = email => email.match(/^[\w-.]+@[\w-]+\.[a-z]{2,}$/i);
Cconst validateEmail = email => email.includes('@') && email.includes('.');
Dconst validateEmail = email => /^[\w-.]+@[\w-]+\.[a-z]{2,}$/i.test(email);
Attempts:
2 left
💡 Hint

Look for the method that returns true or false for regex matching.

lifecycle
advanced
2:00remaining
When does form validation typically run in React Native controlled components?

In a controlled React Native form, at which points is validation usually triggered?

AOnly when the input loses focus (blur event).
BOn every input change and on form submission.
COnly when the component mounts.
DOnly when the user submits the form.
Attempts:
2 left
💡 Hint

Think about giving immediate feedback and final checks.

🔧 Debug
advanced
2:00remaining
Why does this React Native form not show validation errors?

Given this code snippet, why might the validation error message never appear?

React Native
const [email, setEmail] = useState('');
const [error, setError] = useState('');

const handleSubmit = () => {
  if (!email.includes('@')) {
    error = 'Invalid email';
  } else {
    setError('');
  }
};
ABecause the error state is assigned directly instead of using setError.
BBecause the email state is not updated on input change.
CBecause the includes method is not valid on strings.
DBecause the handleSubmit function is never called.
Attempts:
2 left
💡 Hint

Check how state variables are updated in React.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using a validation schema library like Yup in React Native forms?

Why would a developer choose to use a validation schema library such as Yup instead of manual validation functions?

AIt automatically fixes invalid inputs without user interaction.
BIt removes the need to handle form state in React Native.
CIt provides a declarative and reusable way to define complex validation rules and error messages.
DIt disables the submit button until all inputs are valid without extra code.
Attempts:
2 left
💡 Hint

Think about code reuse and clarity in validation logic.