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
RegistrationForm
component, create a state object with two properties:name
andemail
. 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
name
field. This function should update thename
property in the state object with the new value. - Create a function to handle input changes for the
email
field. This function should update theemail
property in the state object with the new value. - Implement basic validation for the
name
field. If thename
field is empty, display an error message. - Implement basic validation for the
email
field. If theemail
field does not contain a valid email address (you can use a simple regular expression for this), display an error message. - Render the
RegistrationForm
component inside your mainApp
component.
- 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!