This should be used in the following manner:
Install with the command:
npm install --save textfield-length-validation
Then, in your code:
import { lengthValidation } from 'textfield-length-validation';
let isValid = lengthValidation(input, counter, maxCharacters);
Where:
-
input
is the input field -
counter
could be a<span>
tag to show the character countdown -
maxCharacters
is the maximum number of characters allowed
<input type="text" id="input"/>
<div>
Characters left <span id="textCounter"></span>
</div>
import { lengthValidation } from 'textfield-length-validation';
const input = document.getElementById('input');
const counter = document.getElementById('textCounter');
const maxCharacters = 10;
lengthValidation(input, counter, maxCharacters);
As it returns a boolean
, it can be used to disable a submit button.