inputmask
repository·5.x·Indexed 27 days ago
https://github.com/robinherbots/inputmaskA JavaScript library for creating input masks to enforce predefined formats on user input fields, such as dates, phone numbers, and numeric values. It is compatible with vanilla JavaScript, jQuery, and jqlite. Features include a datetime extension, Colormask for colored mask characters, and support for custom definitions, aliases, and declarative masking via data-inputmask attributes.
What's inside inputmask
- Inputmask is a JavaScript library designed to create input masks for various input types such as dates, numerics, and phone numbers. It ensures users follow a predefined format during data entry. The library is compatible with vanilla JavaScript, jQuery, and jqlite.
Escape special mask characters
5.xTo display special mask characters as static text within the mask, escape them using a backslash (
\).$(document).ready(function () { // This will show 'm months' as the mask pattern $("#months").inputmask("m \\months"); });Access Inputmask documentation and demo
5.xFor detailed documentation, usage examples, and an interactive demo page, visit the official project website.
https://robinherbots.github.io/Inputmask/Install Inputmask via npm or script tags
5.xYou can integrate Inputmask into your project using several methods depending on your environment.
Webpack / npm
Install the package using npm:
npm install inputmask --saveTo use it in your modules:
// CommonJS var Inputmask = require('inputmask'); // ES6 import Inputmask from "inputmask";Classic Web (Script Tags)
Include the files from the
distfolder.With jQuery dependency:
<script src="jquery.js"></script> <script src="jquery.inputmask.js"></script>Vanilla JS (no jQuery):
<script src="inputmask.js"></script>Automatic Binding: To automatically bind masks to inputs with
data-inputmask-...attributes, include the binding script:<script src="bindings/inputmask.binding.js"></script>npm install inputmask --saveUse Colormask to apply colored masks to inputs
5.xColormask functions identically to
Inputmaskbut allows you to specify a color for the mask characters, which is useful for distinguishing the mask from the user-entered text.To customize the appearance, refer to the
colormask.cssstylesheet to adjust the mask colors to your requirements.Apply masks via data-inputmask attributes
5.xYou can apply masks directly in HTML using the
data-inputmaskattribute.Important: The attribute content must be a well-formed JSON string and should not include the outer curly braces
{}.To initialize these masks, call
inputmask()on the desired elements in your JavaScript code.Run the development server with `npm start`
5.xRunnpm startto start the application in development mode. The app will be available at http://localhost:3000. The page automatically reloads when you make changes, and lint errors will appear in the console.npm startSet initial values for numeric inputs
5.xWhen initializing a mask with a value, the interpretation depends on the
inputTypeoption:- If
inputTypeistext: The value must match theradixPointsymbol (e.g., ifradixPointis,, the value must use,). - If
inputTypeisnumber: The value must use.as the radix point.
Note on jQuery: Using `$(
/html <input name="npt" value="123456,789"/> //js Inputmask("decimal", { radixPoint: ',', inputtype: "text" }).mask("input"); // Using jQuery (Note: jQuery converts numbers to strings, which may break radixPoint logic) $("input").val("123456,789"); $("input").val(123456.789); // Using Vanilla JS (Recommended for precision) document.getElementsByName("npt")[0].value = "123456,789"; document.getElementsByName("npt")[0].value = 123456.789; // type number- If
Install InputMask via NuGet for .NET
5.xTo use InputMask in a .NET project, install the
InputMaskpackage using the Package Manager Console.PM> Install-Package InputMaskBuild the production application with `npm run build`
5.xRunnpm run buildto create a production-ready version of the app in thebuildfolder. This command bundles React in production mode and optimizes the build for performance by minifying files and including content hashes in filenames.npm run buildSet up Colormask in a classic web environment
5.xTo use Colormask in a traditional web project without a module bundler, include the CSS and JavaScript files from the
distfolder using<link>and<script>tags.<link rel="stylesheet" href="colormask.css" /> <script src="colormask.js"></script>Apply masks via data-inputmask-<option> attributes
5.xEvery Inputmask option can be passed individually using the
data-inputmask-<option>naming convention. This is an alternative to the singledata-inputmaskJSON string.To activate these, call
inputmask()on the elements via JavaScript.