generated from jric11/baseProject
18 lines
796 B
JavaScript
18 lines
796 B
JavaScript
/* HTML maxlength counts UTF-16 units; the server counts Unicode code points. */
|
|
(function () {
|
|
'use strict';
|
|
function initialize() {
|
|
document.querySelectorAll('[data-character-limit]').forEach(function (field) {
|
|
const limit = Number(field.dataset.characterLimit);
|
|
function validate() {
|
|
const tooLong = Array.from(field.value.replace(/^[ \t\n\r\0\v]+|[ \t\n\r\0\v]+$/g, '')).length > limit;
|
|
field.setCustomValidity(tooLong ? 'Please enter ' + limit + ' characters or fewer.' : '');
|
|
}
|
|
field.addEventListener('input', validate);
|
|
validate();
|
|
});
|
|
}
|
|
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', initialize);
|
|
else initialize();
|
|
}());
|