RegexCraft is a powerful utility class for building and managing regular expressions with a fluent, chainable API. It simplifies the process of creating complex regex patterns while providing clear validation messages.
- 🔗 Chainable API for building complex patterns
- 📝 Built-in validation presets for common use cases
- 🎯 Custom error messages for each validation rule
- 📱 Phone number validation for multiple countries (RW, DRC, US, UK, KE, UG, TZ, NG, ZA, GH)
- 🔒 Password strength validation
- 👤 Username format validation
- 📧 Email validation
- 🌐 URL validation
- 📅 Date format validation
- 🔍 Pattern testing and visualization
- Node.js 12.x or higher
- npm 6.x or higher
npm install regexcraft
# or
yarn add regexcraft
import RegexCraft from "regexcraft";
// Create a new instance
const regex = new RegexCraft();
// Test a single value
const result = regex
.hasMinLength(8)
.hasUpperCase(1)
.hasLowerCase(1)
.hasNumber(1)
.hasSpecialCharacter(1)
.testOne("MyPassword123!");
console.log(result);
// { value: "MyPassword123!", isValid: true, failedRequirements: [] }
// Test multiple values (still supported)
const results = regex.test(["MyPassword123!", "weak"]);
const passwordValidator = new RegexCraft()
.usePreset("password", "medium") // Use built-in presets
.build();
// Or create custom rules
const customPassword = new RegexCraft()
.hasMinLength(10)
.hasUpperCase(2)
.hasNumber(2)
.hasSpecialCharacter(1)
.build();
const usernameValidator = new RegexCraft()
.usePreset("username", "standard")
.build();
const emailValidator = new RegexCraft().isEmail().build();
const phoneValidator = new RegexCraft()
.isPhone("US") // Supports: US, UK, RW, DRC, KE, UG, TZ, NG, ZA, GH, international
.build();
// Test a single phone number
const result = phoneValidator.testOne("+1234567890");
// Test multiple numbers
const results = phoneValidator.test(["+1234567890", "+254712345678"]);
const formValidator = new RegexCraft()
.field("email", "required|email")
.field("password", "password:high")
.field("username", "username:strict")
.build();
import RegexCraft from "regexcraft";
import { useState } from "react";
function SignupForm() {
const [formData, setFormData] = useState({
username: "",
password: "",
email: "",
});
const [errors, setErrors] = useState({});
const validate = (field, value) => {
const validator = new RegexCraft().usePreset(field, "high").build();
const result = validator.test([value]);
if (!result) {
setErrors((prevErrors) => ({
...prevErrors,
[field]: validator.getErrorMessage(),
}));
}
};
const handleChange = (e) => {
const { name, value } = e.target;
setFormData((prevData) => ({ ...prevData, [name]: value }));
validate(name, value);
};
return (
<form>
<input
type="text"
name="username"
value={formData.username}
onChange={handleChange}
/>
<input
type="password"
name="password"
value={formData.password}
onChange={handleChange}
/>
<input
type="email"
name="email"
value={formData.email}
onChange={handleChange}
/>
{errors.username && <p>{errors.username}</p>}
{errors.password && <p>{errors.password}</p>}
{errors.email && <p>{errors.email}</p>}
</form>
);
}
export default SignupForm;
const form = document.querySelector("#signupForm");
const emailInput = form.querySelector('input[name="email"]');
const errorContainer = form.querySelector(".error-messages");
const validator = new RegexCraft();
emailInput.addEventListener("input", (e) => {
const email = e.target.value;
validator.field("email", "required|email");
const result = validator.test([email])[0];
errorContainer.innerHTML = "";
if (!result.isValid) {
result.failedRequirements.forEach((error) => {
const errorElement = document.createElement("p");
errorElement.textContent = error;
errorContainer.appendChild(errorElement);
});
}
});
Method | Description | Example |
---|---|---|
hasMinLength(length, message?) |
Validates minimum length | .hasMinLength(8) |
hasMaxLength(length, message?) |
Validates maximum length | .hasMaxLength(20) |
hasLengthBetween(min, max, message?) |
Validates length range | .hasLengthBetween(8, 20) |
hasExactLength(length, message?) |
Validates exact length | .hasExactLength(10) |
Method | Description | Example |
---|---|---|
hasLetter(count?, message?) |
Requires letters | .hasLetter(2) |
hasLowerCase(count?, message?) |
Requires lowercase letters | .hasLowerCase() |
hasUpperCase(count?, message?) |
Requires uppercase letters | .hasUpperCase(1) |
hasNumber(count?, message?) |
Requires numbers | .hasNumber(2) |
hasSpecialCharacter(count?, message?) |
Requires special characters | .hasSpecialCharacter() |
Method | Description | Example |
---|---|---|
isEmail(message?) |
Validates email format | .isEmail() |
isURL(options?, message?) |
Validates URL format | .isURL({ protocol: true }) |
isIPv4(message?) |
Validates IPv4 address | .isIPv4() |
isDate(format?, message?) |
Validates date format | .isDate('YYYY-MM-DD') |
isPhone(country?, message?) |
Validates phone numbers | .isPhone('US') |
// Available preset types and levels
const presets = {
password: ["low", "medium", "high"],
username: ["standard", "strict"],
};
// Usage
regex.usePreset("password", "high");
// Test multiple values
const results = regex.test(["test1", "test2"]);
// Visualize the pattern
const visualization = regex.visualize();
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you find this project helpful, please consider giving it a ⭐️ on GitHub!
Eliezer Nsengi
- GitHub: @iAmNsengi
- Thanks to all contributors who have helped shape RegexCraft
- Inspired by the need for simpler regex pattern creation