easy-form-validations
v1.0.5
Published
A simple collection of reusable form validation rules for JavaScript/TypeScript
Maintainers
Readme
📦 easy-form-validations
A lightweight and reusable collection of form validation rules for JavaScript & TypeScript.
Designed to make form validation simple, declarative, and reusable for any project (React, Node.js, Vanilla JS).
🚀 Installation
npm install easy-form-validations✨ Features
- 🔹 Ready-to-use rules (
required,minLength,validEmail,strongPassword, etc.) - 🔹 Works with strings, numbers, dates, email, and password
- 🔹 Supports custom rules (
regex,custom,oneOf) - 🔹 Framework-agnostic (Vanilla JS, TS, React, Next.js, Node.js)
- 🔹 Built-in React hook:
useFormValidation🚀
⚡ Quick Start
1️⃣ Single Field Validation
import { required, validEmail } from "easy-form-validations";
const email = "[email protected]";
const rules = [required(), validEmail()];
rules.forEach(rule => {
if (!rule.validate(email)) {
console.log("❌", rule.message);
}
});
// ✅ No errors2️⃣ Validating an Entire Form
import { validateForm, required, validEmail, minLength, strongPassword } from "easy-form-validations";
const formValues = {
firstName: "John",
email: "[email protected]",
password: "weakpass"
};
const rules = {
firstName: [required()],
email: [required(), validEmail()],
password: [required(), minLength(8), strongPassword()]
};
const errors = validateForm(formValues, rules);
console.log(errors);
// { password: "Password must contain uppercase, lowercase, number and special character" }3️⃣ Using with React (Recommended)
import React from "react";
import { useFormValidation, required, validEmail, minLength, strongPassword } from "easy-form-validations";
function SignupForm() {
const { values, errors, handleChange, validateForm } = useFormValidation({
firstName: "",
email: "",
password: ""
});
const rules = {
firstName: [required()],
email: [required(), validEmail()],
password: [required(), minLength(8), strongPassword()]
};
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (validateForm(rules)) {
alert("✅ Form submitted successfully!");
}
};
return (
<form onSubmit={handleSubmit} style={{ maxWidth: "400px" }}>
<div>
<label>First Name</label><br />
<input name="firstName" value={values.firstName} onChange={handleChange} />
{errors.firstName && <p style={{ color: "red" }}>{errors.firstName}</p>}
</div>
<div>
<label>Email</label><br />
<input name="email" value={values.email} onChange={handleChange} />
{errors.email && <p style={{ color: "red" }}>{errors.email}</p>}
</div>
<div>
<label>Password</label><br />
<input type="password" name="password" value={values.password} onChange={handleChange} />
{errors.password && <p style={{ color: "red" }}>{errors.password}</p>}
</div>
<button type="submit">Submit</button>
</form>
);
}📖 API Reference
📝 Text Rules
| Rule | Description | Example |
|------|-------------|---------|
| required(message?) | Field must not be empty | required("Name required") |
| minLength(min, message?) | Minimum characters | minLength(3) |
| maxLength(max, message?) | Maximum characters | maxLength(20) |
| exactLength(length, message?) | Exact characters | exactLength(10) |
| noNumbers(message?) | Disallow numbers | noNumbers() |
| noSpecialChars(message?) | Disallow special chars | noSpecialChars() |
🔢 Number Rules
| Rule | Description |
|------|-------------|
| isNumber() | Must be a number |
| minValue(min) | Minimum value |
| maxValue(max) | Maximum value |
| isInteger() | Must be integer |
| isPositive() | Must be positive |
📅 Date Rules
| Rule | Description |
|------|-------------|
| isDate() | Must be a valid date |
| minDate(date) | Must be after given date |
| maxDate(date) | Must be before given date |
| isFutureDate() | Must be in the future |
| isPastDate() | Must be in the past |
📧 Email Rules
validEmail(message?)emailDomain(domain, message?)
🔑 Password Rules
strongPassword(message?)passwordMatch(field, message?)
🎯 Misc
regex(pattern, message?)custom(fn, message)oneOf(options[], message?)
📦 Advanced Example: Custom Rule
import { custom } from "easy-form-validations";
const startsWithA = custom((value) => value.startsWith("A"), "Must start with A");
console.log(startsWithA.validate("Adam")); // true
console.log(startsWithA.validate("John")); // false, "Must start with A"🧑💻 Author
Made with ❤️ to make form handling easy & reusable.
Licensed under MIT.
