In this exercise, you'll create a simple form that allows users to input their name and email address. The form should have basic validation to ensure that the name field is not empty and the email field contains a valid email address.
- Create a new React component called
RegistrationForm. - Inside the
RegistrationFormcomponent, create a state object with two properties:nameandemail. Initialize them with empty strings. - Create a function to handle form submission. This function should prevent the default form submission behavior and log the current state object to the console.
- Create a function to handle input changes for the
namefield. This function should update thenameproperty in the state object with the new value. - Create a function to handle input changes for the
emailfield. This function should update theemailproperty in the state object with the new value. - Implement basic validation for the
namefield. If thenamefield is empty, display an error message. - Implement basic validation for the
emailfield. If theemailfield does not contain a valid email address (you can use a simple regular expression for this), display an error message. - Render the
RegistrationFormcomponent inside your mainAppcomponent.
- Add a checkbox to the form that allows users to sign up for a newsletter.
- Implement form reset functionality after successful submission.
- Style the form and error messages using CSS.
To test your solution, follow these steps:
- Run your React application and open it in your web browser.
- Try submitting the form with an empty name field and an invalid email address. Ensure that the appropriate error messages are displayed.
- Submit the form with valid input data. Ensure that the state object is logged to the console correctly.
- If you completed the bonus tasks, test the newsletter checkbox functionality and form reset functionality.
Good luck!