rich-validator
v2.0.2
Published
Client Side Form Validator
Downloads
84
Maintainers
Readme
Client Side From Validation
Introduction
why we provide this library
Validation is a very important to make sure the data is valid. However, if you leave the burden of data validation on the server alone, you will be wasting a lot of time and resources.
Because the server can only validate the data by sending a request, and this consumes a lot of time and resources, there must be a way to validate the data before sending the request to the server.
Why Vanilla Javascript
JavaScript is efficient for this task, so we relied on it in developing this library. We also wrote it in vanilla JavaScript so that it is compatible with your programming language, no matter which one you use to develop your system.
Installation
npm i rich-validator
Usage
:warning: The package return bool (True - False) .
import { validator } from 'rich-validator';
let targetForm = document.querySelector('.targetForm');
let MainFormValidation = validator(targetForm); // Fire Validation
if (MainFormValidation) {
// Do Something if no validation errors
} else {
// Do Something form has validation errors
}
Features
- Required Input
- Email Input
- Mobile Number Validation
- Strong Password Validation
- force max length for input by default (255)
Validation Classes
:warning: The package has a global rule for any input in the targeted with [type='text'] to make max length 255 character.
if you want change the default max length you must add ths
<input type='text' rich-length='190' />
Validation Rules
// For make any field has this class not empty
querySelector('.required');
// For make sure input has this class is a phone number and valid format
querySelector('.phone-number');
// For make sure input has this class is a strong password
querySelector('.password');
// For make sure input has this class is a valid email address
querySelector('.validate-email');
// For make sure uploaded file has a valid extension [jpg,jpeg,png,webp]
querySelector('.validate-image');
// For make sure uploaded file has a valid extension [text,txt]
querySelector('.validate-text');
// For make sure uploaded file has a valid extension [pdf]
querySelector('.validate-pdf');
// For make sure uploaded file has a valid extension [doc,docx]
querySelector('.validate-doc');
// Validate uploaded file size
querySelector('.validate-size');
and add size attribute
<input type='file' data-size='4000' /> // 4 MB
Error Handling
You Must put your input in the parent div like this example
<div>
<input type='text' class='required' />
</div>
if our library detect validation error for this input we append error message in the parent container like this example.
<div>
<label for="slogan">Slogan</label>
<input class="required" id="slogan" type="text" name="slogan" placeholder="Slogan" />
<label class="validation-error">Slogan is required</label>
</div>
Customize error messages
<!-- Customize Error Messages-->
<!-- Required Validation Error -->
<input
type='text'
data-required-error='Custom Required Error Message'
class='required' />
<!-- Phone Number Validation Error -->
<input
type='text'
data-phone-error='Custom Phone Number Error Message'
class='phone-number' />
<!-- Email Validation Error -->
<input
type='text'
data-email-error='Custom Email Error Message'
class='validate-email' />
<!-- Image Validation Error -->
<input
type='file'
data-image-error='Custom Image Error Message'
class='validate-image' />
<!-- Text File Validation Error -->
<input
type='file'
data-text-error='Custom Text File Error Message'
class='validate-text' />
<!-- PDF File Validation Error -->
<input
type='file'
data-pdf-error='Custom PDF File Error Message'
class='validate-pdf' />
<!-- DOCS File Validation Error -->
<input
type='file'
data-doc-error='Custom DOCS File Error Message'
class='validate-pdf' />
Customize password validation error messages
<!-- Custom Password Min Length Error Message -->
<input
type='password'
data-password-error-min-length='Custom Password Min Length Error Message'
class='password' />
<!-- Custom Password Max Length Error Message -->
<input
type='password'
data-password-error-max-length='Custom Password Max Length Error Message'
class='password' />
<!-- Custom Password Minimum One Character Lowercase Error Message -->
<input
type='password'
data-password-error-lowercase='Custom Password Minimum One Character Lowercase Error Message'
class='password' />
<!-- Custom Password Minimum One Character uppercase Error Message -->
<input
type='password'
data-password-error-uppercase='Custom Password Minimum One Character uppercase Error Message'
class='password' />
<!-- Custom Password Minimum One Character uppercase Error Message -->
<input
type='password'
data-password-error-number='Custom Password Minimum One Number Error Message'
class='password' />
<!-- Custom Password Minimum One Spatial Character Error Message -->
<input
type='password'
data-password-error-spatial='Custom Password Minimum One Spatial Character Error Message'
class='password' />