28 lines
849 B
JavaScript
28 lines
849 B
JavaScript
/**
|
|
* @param {HTMLElement} element
|
|
* @param {RegExp} regex
|
|
* @returns {HTMLElement}
|
|
*/
|
|
export function validation(element, regex) {
|
|
if (element instanceof HTMLElement && regex instanceof RegExp) {
|
|
element.addEventListener('change', e => {
|
|
const target = e.target;
|
|
if (target instanceof HTMLElement) {
|
|
if (regex.test(/** @type {HTMLInputElement} */(target).value)) {
|
|
target.classList.remove('validation-error');
|
|
} else {
|
|
target.classList.add('validation-error');
|
|
}
|
|
}
|
|
})
|
|
}
|
|
return element;
|
|
}
|
|
|
|
/**
|
|
* @param {{[key: string]: string}} style
|
|
* @returns {string}
|
|
*/
|
|
export function convertCssStyle(style) {
|
|
return Object.entries(style).map(([key, value]) => `${key}: ${value}`).join('; ');
|
|
} |