Hone logo
Hone
Problems

Angular Form Validation Challenge: User Registration

This challenge focuses on implementing robust form validation in an Angular application for a user registration form. Form validation is crucial for ensuring data integrity and providing a user-friendly experience by guiding users to correct their input before submission. You'll build a form with various input types and validation rules to demonstrate your understanding of Angular's reactive forms approach.

Problem Description

You are tasked with creating an Angular component that displays a user registration form. The form should include the following fields:

  • Username: Required, minimum 3 characters, maximum 20 characters.
  • Email: Required, must be a valid email format.
  • Password: Required, minimum 8 characters, must contain at least one uppercase letter, one lowercase letter, and one number.
  • Confirm Password: Required, must match the Password field.
  • Terms and Conditions: Required, a checkbox that must be checked before submission.

The component should use Angular's reactive forms approach. Implement validators to enforce the specified rules. The form should display appropriate error messages next to each field when validation fails. Upon successful validation, a "Submit" button should be enabled. When the form is submitted (even if validation is successful), a console log message should indicate successful submission. The form should not actually submit data to a server; the focus is on the validation logic.

Key Requirements:

  • Use Angular's reactive forms.
  • Implement custom validators for Username and Password.
  • Display clear and concise error messages for each validation failure.
  • Enable the "Submit" button only when the form is valid.
  • Handle the form submission (logging a success message).
  • Ensure the Confirm Password field matches the Password field.
  • Handle the Terms and Conditions checkbox requirement.

Expected Behavior:

  • When a field is invalid, an error message should appear immediately next to the field.
  • Error messages should disappear when the field becomes valid.
  • The "Submit" button should be disabled when the form is invalid and enabled when the form is valid.
  • Clicking the "Submit" button (when valid) should log "Form submitted successfully!" to the console.
  • The form should be visually appealing and easy to use.

Edge Cases to Consider:

  • Empty input fields.
  • Invalid email formats (e.g., missing "@" symbol, invalid domain).
  • Passwords that do not meet the complexity requirements.
  • Mismatched password and confirm password fields.
  • Unchecked Terms and Conditions checkbox.
  • Username exceeding the maximum character limit.

Examples

Example 1:

Input: Username: "ab", Email: "invalid-email", Password: "short", Confirm Password: "short", Terms and Conditions: unchecked
Output: Error messages displayed for Username (too short), Email (invalid format), Password (too short), Confirm Password (too short), and Terms and Conditions (required). Submit button disabled.
Explanation: All fields have validation errors, preventing form submission.

Example 2:

Input: Username: "validUser", Email: "test@example.com", Password: "StrongPass1!", Confirm Password: "StrongPass1!", Terms and Conditions: checked
Output: No error messages displayed. Submit button enabled.  Console log: "Form submitted successfully!"
Explanation: All fields are valid, allowing form submission and triggering the success message.

Example 3:

Input: Username: "verylongusernameexceedinglimit", Email: "test@example.com", Password: "StrongPass1!", Confirm Password: "StrongPass1!", Terms and Conditions: checked
Output: Error message displayed for Username (too long). Submit button disabled.
Explanation: Username exceeds the maximum length, preventing form submission.

Constraints

  • The solution must be implemented using Angular's reactive forms.
  • The component should be self-contained and reusable.
  • The solution should be well-structured and easy to understand.
  • The component should be responsive and work correctly on different screen sizes.
  • No external libraries beyond Angular itself are allowed for validation. Custom validators are encouraged.
  • The solution should be written in TypeScript.

Notes

  • Consider using Angular's Validators class for built-in validators like Validators.required, Validators.email, etc.
  • For custom validators, you can create custom validator functions and add them to the form controls.
  • Use Angular's FormGroup, FormControl, and FormBuilder classes to manage the form.
  • Utilize Angular's template syntax to display error messages dynamically.
  • Think about how to handle the Terms and Conditions checkbox validation effectively.
  • Focus on creating a clean and maintainable solution. Good coding practices are important.
Loading editor...
typescript