На этом сайте используется плагин, который в поле телефона позволяет вводить только цифры. Можно ли для поля имени сделать ввод только русских букв и пробелов? Никаких плагинов, позволяющих это реализовать, найти не могу.
3 ответа
<input type='text' id='abbr'>
И javascript:
// document.getElementById('abbr').addEventListener('keypress', fn, false);
$('#abbr').on('keypress', function() {
var that = this;
setTimeout(function() {
var res = /[^а-я ]/g.exec(that.value);
console.log(res);
that.value = that.value.replace(res, '');
}, 0);
});
HTML5
<style>
input:invalid {
border-color: red;
}
</style>
<form>
<input type="text" pattern="[А-Яа-яЁё ]+"/>
<input type="submit">
</form>
Вот немного подправленный пример с поддержкой заглавных букв и украинских символов (но принцип тот же):
$(function() {
jQuery(function($) {
$('#cyr').on('keypress', function() {
var that = this;
setTimeout(function() {
var res = /[^а-яА-ЯїЇєЄіІёЁ ]/g.exec(that.value);
that.value = that.value.replace(res, '');
}, 0);
});
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type=text id=cyr />