form-validator-plugin v1.0.1
form-validator-plugin
A lightweight and flexible jQuery plugin for form validation with customizable rules and messages.
Features
- Required field validation
- Alphanumeric, full-width character, and numeric validation
- Minimum, maximum, and range length validation
- Custom regex pattern validation
- Email, URL, phone number, and postal code format validation
- Conditional validation and skipping fields
- Custom validation rules
- Customizable error messages and feedback display
Installation
Via npm
npm install form-validator-plugin
Manual Installation
Include jQuery and the formValidatorPlugin
plugin in your HTML file:
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="path/to/dist/form-validator-plugin.min.js"></script>
<link rel="stylesheet" href="path/to/dist/styles.min.css">
If you are using Bootstrap, include it as well:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.3.3/css/bootstrap.min.css">
Usage
Initialize the plugin on your form element:
$(document).ready(function() {
$('#myForm').formValidatorPlugin({
rules: {
fieldName: {
required: true,
minLength: 5
},
anotherField: {
email: true
}
},
messages: {
fieldName: {
required: "This field is required",
minLength: "This field must be at least {minLength} characters long"
},
anotherField: {
email: "Please enter a valid email address"
}
},
triggerButtonId: "submitBtn", // Only necessary if type="button"
onValid: function() {
alert("Form is valid!");
},
onInvalid: function() {
alert("Form is invalid!");
}
});
});
Options
rules
An object defining the validation rules for each form field. Available rules:
required
: Ensures the field is not empty.- Example:
rules: { fieldName: { required: true } }
- Example:
bothRequired
: Ensures both fields are not empty.- Example:
rules: { fieldName: { bothRequired: "#anotherField" }, anotherField: { bothRequired: "#fieldName" } }
- Example:
checkbox
: Ensures a checkbox is checked.- Example:
rules: { acceptTerms: { checkbox: true } }
- Example:
alphaNum
: Ensures the field contains only alphanumeric characters.- Example:
rules: { username: { alphaNum: true } }
- Example:
fullWidthChars
: Ensures the field contains only full-width characters.- Example:
rules: { japaneseText: { fullWidthChars: true } }
- Example:
noNumbers
: Ensures the field contains no numeric characters.- Example:
rules: { name: { noNumbers: true } }
- Example:
minLength
: Ensures the field has at least the specified number of characters.- Example:
rules: { password: { minLength: 8 } }
- Example:
maxLength
: Ensures the field has no more than the specified number of characters.- Example:
rules: { description: { maxLength: 100 } }
- Example:
rangeLength
: Ensures the field length is within the specified range.- Example:
rules: { username: { rangeLength: [5, 15] } }
- Example:
pattern
: Ensures the field matches the specified regex pattern.- Example:
rules: { zipCode: { pattern: "^[0-9]{3}-[0-9]{4}$" } }
- Example:
numeric
: Ensures the field contains a numeric value.- Example:
rules: { age: { numeric: true } }
- Example:
integer
: Ensures the field contains an integer value.- Example:
rules: { quantity: { integer: true } }
- Example:
min
: Ensures the field value is at least the specified number.- Example:
rules: { age: { min: 18 } }
- Example:
max
: Ensures the field value is no more than the specified number.- Example:
rules: { score: { max: 100 } }
- Example:
range
: Ensures the field value is within the specified range.- Example:
rules: { score: { range: [0, 100] } }
- Example:
email
: Ensures the field contains a valid email address.- Example:
rules: { email: { email: true } }
- Example:
url
: Ensures the field contains a valid URL.- Example:
rules: { website: { url: true } }
- Example:
phone
: Ensures the field contains a valid phone number.- Example:
rules: { phoneNumber: { phone: true } }
- Example:
postalCode
: Ensures the field contains a valid postal code.- Example:
rules: { postalCode: { postalCode: true } }
- Example:
date
: Ensures the field contains a date in YYYY-MM-DD format.- Example:
rules: { birthDate: { date: true } }
- Example:
dateRange
: Ensures the date range is valid.- Example:
rules: { startDate: { date: true, dateRange: ["#startDate", "#endDate"] }, endDate: { date: true, dateRange: ["#startDate", "#endDate"] } }
- Example:
validOption
: Ensures the field value is one of the specified valid options.- Example:
rules: { country: { validOption: ["US", "CA", "MX"] } }
- Example:
messages
An object defining custom error messages for each validation rule.
customValidators
An object defining custom validation functions.
feedbackSelectors
An object defining custom selectors for feedback display elements.
triggerButtonId
The ID of the button that triggers the form validation. Defaults to "submitBtn"
. Only necessary for buttons with type="button"
. Not required for submit
buttons.
conditionalSkips
Settings to skip field validation based on specific conditions. The following conditions are supported:
- skipIfNotEmpty: Skips validation if the dependent field is not empty.
- skipIfValue: Skips validation if the dependent field has a specific value.
- skipIfChecked: Skips validation if the dependent field is checked.
Example
conditionalSkips: {
fieldName: {
skipIfNotEmpty: true,
dependentId: "anotherField"
},
anotherField: {
skipIfValue: "specificValue",
dependentId: "someOtherField"
},
checkboxField: {
skipIfChecked: true,
dependentId: "yetAnotherField"
}
}
onValid
A callback function executed when the form is valid.
onInvalid
A callback function executed when the form is invalid.
Example
<form id="myForm">
<div class="form-group">
<label for="fieldName">Field Name</label>
<input type="text" id="fieldName" name="fieldName" class="form-control">
<div class="invalid-feedback"></div>
</div>
<div class="form-group">
<label for="anotherField">Another Field</label>
<input type="email" id="anotherField" name="anotherField" class="form-control">
<div class="invalid-feedback"></div>
</div>
<button type="button" id="submitBtn">Submit</button>
</form>
$(document).ready(function() {
$('#myForm').formValidatorPlugin({
rules: {
fieldName: {
required: true,
minLength: 5
},
anotherField: {
email: true
}
},
messages: {
fieldName: {
required: "This field is required",
minLength: "This field must be at least {minLength} characters long"
},
anotherField: {
email: "Please enter a valid email address"
}
},
triggerButtonId: "submitBtn", // Only necessary if type="button"
onValid: function() {
alert("Form is valid!");
},
onInvalid: function() {
alert("Form is invalid!");
}
});
});
License
This project is licensed under the MIT License.