Codetive
Sign In
Coding challenges as of now are only available on desktop
On mobile you can try our quiz
Quiz
We are working hard to bring coding challenges with editor on mobile 💚

Mini Form Validation Library

Objective: Implement a simple form validation library that allows validating form fields based on custom rules.

Instructions:

  1. Create a function named validateForm that takes an object representing form data and an object representing validation rules.
  2. The function should validate each field based on the provided rules and return a result indicating whether the form is valid and what errors (if any) were found.
  3. The validation rules should support checks for:
    • Required fields
    • Minimum/maximum length for text fields
    • Email format validation

Example Usage:

const formData = {
  username: 'user123',
  password: 'pass',
  email: 'user@example.com',
};
 
const validationRules = {
  username: { required: true, minLength: 5 },
  password: { required: true, minLength: 8 },
  email: { required: true, email: true },
};
 
const result = validateForm(formData, validationRules);
console.log(result);
// Output: { valid: false, errors: { password: 'Password must be at least 8 characters.' } }

Requirements:

  • The function should validate based on rules like required, minLength, and email format.
  • Return an object with a valid boolean and an errors object indicating what validation failed.

Bonus:

  1. Add support for validating multiple types of fields (e.g., numbers, dates).
Browser
Console
Tests
Soon