限制输入字段的多语言 [英] Restricting Multilanguages for input field

查看:94
本文介绍了限制输入字段的多语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务是限制非英语语言的输入字段。只有英语才能进入该字段。

I have got a task to restrict the input field from non English languages.Only English should be enter on the field.

我的文本框是

<input type="text"/>

该功能是

 $(document).on("keypress", "input[type='text'] function (event) {
        return suppressNonEng(event);
    });

function suppressNonEng(EventKey) {
    var key = EventKey.which || EventKey.keyCode;
    if (key > 128) { sefAlert("Only English is allowed"); return false; }
    else { return true; }
}

在中文,希腊文和其他一些情况下也是如此。但是在西班牙语,法语的情况下,它不起作用,因为在英语和法语中使用相同的ASCII字符。有没有解决这个问题的方法?请帮助

Its worked in the case of Chinese,Greek and some other also.But in the case of Spanish,French, its not working because the same ASCII character is used in the English and French. Is there any solution for this problem?please help

推荐答案

小提琴

非常简单。您需要将输入的每个字符与正则表达式匹配,以检查输入的字符是否来自英文字母。

Its pretty simple. You need to match every character entered with a regex that checks whether the character entered is from the English alphabet, or not.

$("#mytextbox").on("keypress", function(event) {
    var englishAlphabetAndWhiteSpace = /[A-Za-z ]/g;
    var key = String.fromCharCode(event.which);
    if (englishAlphabetAndWhiteSpace.test(key)) {
        return true;
    }
    alert ("this is not in English");//put any message here!!!
});

发表评论后:

每一把钥匙在键盘上有一个键码。因此,当您按下类似E的键时,计算机会将其解释为键码(在本例中为69)。很难让计算机理解法语E或英语E之间的区别。

Every key on the keyboard has a keycode. So when you press a key like E, the computer will interpret it as a keycode (69, in this case). It's difficult to make the computer understand the difference between French E or English E.

如果您不想提醒用户,只需用 return false; 。

If you dont want to alert the user, just replace the alert with return false;.

如果您需要检测浏览器语言

使用此:

var userLang = navigator.language || navigator.userLanguage; 
alert ("The language you are using is: " + userLang);
if(userLang!=whatever-you-want){
    alert("only whatever-you-want allowed!!!")
}

检查您的语言

这篇关于限制输入字段的多语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆