Unlike bulky validation solutions, it weighs less than 5KB minified while providing comprehensive validation capabilities through HTML data attributes.
You use it to validate user input in real-time or on demand, handle various input types, and customize error feedback without writing complex validation logic in your JS files.
data-strictly-*).oninput) or just show errors (showerror).div/p), and CSS classes.data-strictly-equalto).1. Download and load the minified version of the Strictly.js library in the document.
<script src="strictly.min.js"></script>
2. Initialize the Strictly.js library and pass a CSS selector for the element(s) containing the inputs you want to validate. This can be a form, a div, or even a specific input.
const instance = new Strictly('.form', {
// 'showerror' or 'oninput'
restrict: 'showerror',
// custom classes
fieldErrorClass: 'strictly-validation-error',
fieldSuccessClass: 'strictly-validation-success',
errorClass: 'strictly-error-message',
// 'p' or 'div'
errorTag: 'p', // Allowed values:
// 'down', 'up', or 'custom'
errorMessagePosition: 'down',
// CSS class of parent element if errorMessagePosition is 'custom'.
errorCustomClass: null,
// shows error immediately on input
initError: true,
}); 3. Customize the styles of error/success states and error messages:
.strictly-validation-error {
border: 1px solid #FF0000;
}
.strictly-validation-success {
border: 1px solid #00FF00;
}
.strictly-error-message {
font-size: 12px;
color: #FF0000;
font-weight: 500;
text-transform: capitalize;
margin: 4px 0;
} 4. Add validation rules directly to your HTML elements using the following HTML data attributes:
data-strictly-required="true|false": Explicitly mark a field as required or not required (overrides native `required`).data-strictly-type="email|number|integer|digits|alphanum|alphanumspace|alphanumstrict|url": Specify the expected data format (use instead of or alongside `type` attribute for text inputs).data-strictly-datetime="format": Validate specific date/time formats (e.g., “YYYY-MM-DD HH:mm”).data-strictly-minlength="N": Enforce a minimum character length.data-strictly-maxlength="N": Enforce a maximum character length.data-strictly-length="[min, max]": Enforce a character length within a specific range.data-strictly-min="N": Enforce a minimum numerical value.data-strictly-max="N": Enforce a maximum numerical value.data-strictly-range="[min, max]": Enforce a numerical value within a specific range.data-strictly-pattern="regex": Validate input against a specified regular expression pattern.data-strictly-password="rules": Enforce password complexity rules (e.g., “alpha:caps, num, min=8”).data-strictly-mincheck="N": Require at least N checkboxes with the same `name` attribute to be checked.data-strictly-maxcheck="N": Allow at most N checkboxes with the same `name` attribute to be checked.data-strictly-check="[min, max]": Require the number of checked checkboxes (same `name`) to be within a range.data-strictly-equalto="selector": Require the field’s value to match the value of another field identified by the CSS selector.data-strictly-minwords="N": Require a minimum number of words.data-strictly-maxwords="N": Allow a maximum number of words.data-strictly-words="[min, max]": Require the word count to be within a specific range.data-strictly-mincharacters="N": Require a minimum number of characters (similar to `minlength`).data-strictly-maxcharacters="N": Allow a maximum number of characters (similar to `maxlength`).data-strictly-characters="[min, max]": Require character count to be within a range (similar to `data-strictly-length`).data-strictly-minselect="N": Require at least N options to be selected in a multi-select dropdown.data-strictly-maxselect="N": Allow at most N options to be selected in a multi-select dropdown.data-strictly-select="[min, max]": Require the number of selected options in a multi-select to be within a range.data-strictly-initialnospace: Ensures input does not start with a space.data-strictly-singlespace: Ensures only single spaces are allowed between words.data-strictly-filetype: Allowed file types/extensions (comma-separated, e.g. image/png,.jpg)data-strictly-filesize: Max size in bytes.data-strictly-filecount: Min/max files.data-strictly-custom: Custom form validation.<h3>Basic Inputs</h3> <div class="mb-3"> <label for="field-1" class="form-label">Field 1</label> <input type="text" id="field-1" name="field1" class="form-control" data-strictly-maxlength="4" required> </div> <div class="mb-3"> <label for="field-2" class="form-label">Field 2</label> <input type="text" id="field-2" name="field2" class="form-control" data-strictly-type="url" required> </div> <button id="subb" class="btn btn-primary">Validate</button>
5. Strictly.js provides a public method to trigger validation manually for all targeted elements.
const result = instance.validate({
formError: true
}); required, type="email", pattern, minlength, etc.) but offers limited customization for error messages and styling across browsers. Strictly.js provides more rules out-of-the-box (like alphanumstrict, word counts, checkbox groups) and gives you full control over error display and styling via CSS classes and placement options. Strictly.js is preferable when you need consistent behavior and appearance, or more complex rules than HTML5 provides natively.data-* attributes. It’s generally more feature-rich than Strictly.js, offering things like custom validators, internationalization (i18n), remote validation (AJAX), and potentially more complex grouping/dependency handling. Parsley might be a better choice for very complex forms or enterprise applications needing those advanced features. Strictly.js seems positioned as a simpler, more lightweight alternative focusing on the core attribute-based validation experience. If you just need solid validation without the extra bells and whistles (and potentially larger footprint) of Parsley, Strictly.js is a good fit.v2.0.1 (07/11/2025)
The post Client-Side Declarative Form Validation using Strictly.js appeared first on CSS Script.
They look like your average open earbuds, but with optional RGB LED effects. | Photo…
200 Years Ago By virtue of a warrant from the selectmen of the town of…
Ally Connor, back, and Eva Dentremont, bottom, lounge with Lincoln on their porch as the…
SOUTHAMPTON — Residents could again be asked to decide whether to approve a Proposition 2½ override…
NORTHAMPTON — From limericks to lighthearted jabs, the Hotel Northampton ballroom was transformed Friday morning…
EASTHAMPTON — Surging energy costs put a strain on trying to power two large-scale food…
This website uses cookies.