Ввод только на одном языке
Добрый день. Пытался найти код, позволяющий делать ввод только на одном языке. Суть такая:
"Как только я начинаю вводить например на русском языке, последующий ввод скажем латиницы - запрещен. То же самое и при вводе латиницы. Если поле стало пустым (например удаление символов), должен произойти сброс настроек, так чтобы я смог назначить заново язык". Получилось реализовать ввод только на одном языке, меняя на лету маску. А вот как при опустении поменять поведение - так и не допер. Вот мой г*внокод (не судите строго, я явно не Гуру JS).
(function() {
jQuery.keyboardLayout = {};
jQuery.keyboardLayout.target;
jQuery.keyboardLayout.layout;
jQuery.keyboardLayout.show = function(layout){
this.layout = layout;
};
jQuery.keyboardLayout.hide = function(){
this.target = null;
this.layout = null;
};
jQuery.fn.keyboardLayout = function(){
this.each(function(){
$(this).focus(function(){
jQuery.keyboardLayout.target = $(this);
});
$(this).blur(function(){
jQuery.keyboardLayout.hide();
});
$(this).keypress(function(e){
var c = (e.charCode == undefined ? e.keyCode : e.charCode);
var layout = jQuery.keyboardLayout.layout;
if (c >= 97/*a*/ && c <= 122/*z*/ && !e.shiftKey ||
c >= 65/*A*/ && c <= 90/*Z*/ && e.shiftKey ||
(c == 91/*[*/ && !e.shiftKey ||
c == 93/*]*/ && !e.shiftKey ||
c == 123/*{*/ && e.shiftKey ||
c == 125/*}*/ && e.shiftKey ||
c == 96/*`*/ && !e.shiftKey ||
c == 126/*~*/ && e.shiftKey ||
c == 64/*@*/ && e.shiftKey ||
c == 35/*#*/ && e.shiftKey ||
c == 36/*$*/ && e.shiftKey ||
c == 94/*^*/ && e.shiftKey ||
c == 38/*&*/ && e.shiftKey ||
c == 59/*;*/ && !e.shiftKey ||
c == 39/*'*/ && !e.shiftKey ||
c == 44/*,*/ && !e.shiftKey ||
c == 60/*<*/ && e.shiftKey ||
c == 62/*>*/ && e.shiftKey) && layout != 'EN') {
layout = 'en';
} else if (c >= 65/*A*/ && c <= 90/*Z*/ && !e.shiftKey ||
c >= 97/*a*/ && c <= 122/*z*/ && e.shiftKey) {
layout = 'EN';
} else if (c >= 1072/*а*/ && c <= 1103/*я*/ && !e.shiftKey ||
c >= 1040/*А*/ && c <= 1071/*Я*/ && e.shiftKey ||
(c == 1105/*ё*/ && !e.shiftKey ||
c == 1025/*Ё*/ && e.shiftKey ||
c == 8470/*№*/ && e.shiftKey ||
c == 59/*;*/ && e.shiftKey ||
c == 44/*,*/ && e.shiftKey) && layout != 'RU') {
layout = 'ru';
} else if (c >= 1040/*А*/ && c <= 1071/*Я*/ && !e.shiftKey ||
c >= 1072/*а*/ && c <= 1103/*я*/ && e.shiftKey) {
layout = 'RU';
}
if (layout) {
jQuery.keyboardLayout.show(layout);
}
console.log(layout);
if(!this.value) {
layout = "";
console.log("123");
e.preventDefault();
//$(this).removeClass("rusonly");
//$(this).removeClass("engonly");
}
if(layout == 'ru' || layout == 'RU'){
if ($(this).hasClass("rusonly")) {
} else if ($(this).hasClass("engonly")) {
} else {
$(this).addClass("rusonly");
$(this).removeClass("engonly");
}
} else if(layout == 'en' || layout == 'EN'){
if ($(this).hasClass("engonly")) {
} else if ($(this).hasClass("rusonly")) {
} else {
$(this).addClass("engonly");
$(this).removeClass("rusonly");
}
}
//ввод только английского
$('.engonly').bind('keyup blur',function(){
if($(this).val() != 'Отсутствует'){
$(this).val( $(this).val().replace(/[А-Яа-я]/g,'') );
}
}
);
//ввод только русского
$('.rusonly').bind('keyup blur',function(){
if($(this).val() != 'Отсутствует'){
$(this).val( $(this).val().replace(/[A-Za-z]/g,'') );
}
}
);
});
});
};
})();
Вызываю это всё при помощи:
$(function () {
$('#jur_name').keyboardLayout();
})
Подскажите, может есть готовое решение получше или можно как-то добить этот добавив возможность сброса replace до "по-умолчанию"? |
Sobsoft,
шифровка из центра ...
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
jQuery.fn.keyboardLayout = function() {
var c = /[\u0430-\u044f\u0451]/gi,
d = /[a-z]/gi;
return this.each(function(g, a) {
var b;
$(a).on("input", function() {
if (a.value.length)
if (b) a.value = a.value.replace(b, "");
else {
var e = ~a.value.search(c),
f = ~a.value.search(d);
e && f ? b = e < f ? d : c : e ? b = d : f && (b = c)
}
else b = void 0
})
})
};
$(function() {
$('#jur_name').keyboardLayout();
});
</script>
</head>
<body>
<input type="text" id="jur_name">
</body>
</html>
|
Центр, это Sobsoft. Шифровка получена, отправлена по назначению.
p.s. Спасибо большое. То, что нужно. |
| Часовой пояс GMT +3, время: 04:17. |