A lightweight React component for easy form validation and error handling. This package makes building forms with validation a breeze by providing a clean, declarative API.
- Easy-to-use: Quickly add form validation to your React project.
- Customizable Rules: Define required fields, patterns, and error messages.
- No Page Reload: Prevents page reloads on form submission.
- Error Handling: Display custom error messages for invalid fields.
Install the package via npm:
npm install react-form-validator-pro
Here’s how you can use the package in your React project:
import React from 'react';
import { FormValidator } from 'react-form-validator-pro';
const App = () => {
const handleSubmit = (formData) => {
console.log('Form submitted:', formData);
};
return (
<div>
<h1>React Form Validator Example</h1>
<FormValidator
onSubmit={handleSubmit}
validations={{
username: { required: true, requiredMessage: "Username is required" },
email: {
required: true,
pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
message: 'Invalid email format',
},
phone: {
required: true,
type: 'phone',
typeMessage: 'Invalid phone number format',
minLength: 10,
minLengthMessage: 'Phone number must be at least 10 characters long',
},
password: {
required: true,
type: 'password',
typeMessage: 'Password must contain at least one letter, one number, and one special character',
minLength: 8,
minLengthMessage: 'Password must be at least 8 characters long',
},
}}
validateOnChange={true}
validateOnBlur={true}
>
<div>
<label>Username:</label>
<input name="username" placeholder="Enter your username" />
</div>
<div>
<label>Email:</label>
<input name="email" placeholder="Enter your email" />
</div>
<div>
<label>Phone:</label>
<input name="phone" placeholder="Enter your phone number" />
</div>
<div>
<label>Password:</label>
<input name="password" type="password" placeholder="Enter your password" />
</div>
<button type="submit">Submit</button>
</FormValidator>
</div>
);
};
export default App;
Prop | Type | Default | Description |
---|---|---|---|
children |
node |
Required | The form fields to validate. |
onSubmit |
func |
Required | A callback function that receives the validated form data. |
rules |
object |
{} |
An object defining validation rules for each field. |
Each field in the rules
object should have the following format:
Key | Type | Description |
---|---|---|
required |
boolean |
If true , the field must be filled. |
pattern |
RegExp |
A regular expression to validate the input against. |
message |
string |
Custom error message to display if the pattern check fails. |
You can customize error messages by adding them to the rules
object. For example:
<FormValidator
rules={{
password: {
required: true,
pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/,
message: 'Password must be at least 8 characters long and contain letters and numbers',
},
}}
>
If you'd like to contribute:
- Clone the repository:
git clone https://github.com/mahmud-r-farhan/react-form-validator-pro.git
- Install dependencies:
npm install
- Run tests:
npm test
- Build the package:
npm run build
This project is licensed under the MIT License.
Feel free to open issues or submit pull requests! Feedback is always appreciated. 😊