<p>Этот <code>input</code> не принимает числа!</p>
<input type="text" id="withoutNumbers">
<p>A этот <code>input</code> принимает только буквы!</p>
<input type="text" id="onlyChars">
<p>Тут можно вставлять еще и пробелы!</p>
<input type="text" id="onlyCharsWithSpaces">
body {
margin: 0;
padding: 20px;
font: 16px 'Segoe UI', Arial, Helvetica, sans-serif;
}
p {
margin: 0 0 20px 0;
}
code {
font-weight: 600;
}
input {
margin-bottom: 20px;
padding: 5px;
font: inherit;
}
const setReplacer = (target, expression) => {
target.addEventListener('input', () => {
const parsedValue = target.value.replace(expression, '');
if (parsedValue !== target.value) {
target.value = parsedValue;
}
});
};
setReplacer(document.querySelector('#withoutNumbers'), /\d/g);
setReplacer(document.querySelector('#onlyChars'), /[\W\d]/g);
setReplacer(document.querySelector('#onlyCharsWithSpaces'), /[^A-Za-zА-Яа-я\s]/g);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.