如何检测非“GSM 7位字母表”输入字段中的字符 [英] How to Detect Non "GSM 7 bit alphabet" characters in input field

查看:187
本文介绍了如何检测非“GSM 7位字母表”输入字段中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测文本输入字段是否包含任何不属于GSM 7位字母表的字符。带有字符的表格在这里 http://www.dreamfabric.com/sms/default_alphabet.html

I am trying to detect if a text input field has any character that doesn't belong to the GSM 7 bit alphabet. The table with the characters is here http://www.dreamfabric.com/sms/default_alphabet.html

经过大量搜索后我发现了这一点(我需要用什么正则表达式检查一些非拉丁字符?)它非常接近我想要的完成因为它检测非拉丁字符。如何更改正则表达式以包含GSM 7位字母?

After a lot of searching I found this (What regular expression do I need to check for some non-latin characters?) that its pretty close to what I want to accomplish because It detects Non latin characters. How can I alter the regular expression to include the GSM 7 bit alphabet?

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>test foreign chars</title>
</head>
<body>

    <input id="foreign_characters" size="12" type="text" name="foreign_characters" value="test">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">

(function(){

    $('#foreign_characters').on("keyup", function(){

        var foreignCharacters = $("#foreign_characters").val();
        var rforeign = /[^\u0000-\u007f]/;

        if (rforeign.test(foreignCharacters)) {
          alert("This is non-Latin Characters");
        } else {
          alert("This is Latin Characters");
        }

    });

})();

    </script>
</body>
</html>


推荐答案

您可以将所有有效字符放入字符串中然后重复搜索字符串。

You can put all valid characters in a string and then search the string repeatedly.

gsm = "@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞ^{}\[~]|€ÆæßÉ!\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";
var letter = 'a';
var letterInAlfabet = gsm.indexOf(letter) !== -1;

如果你确定你的编码是正确的使用它,即将您的Javascript文件保存为UTF8,并指定它是浏览器的UTF8

Make sure you get your encodings right if you use this, i.e. save your Javascript file as UTF8 and specify that it is UTF8 to the browser.

这篇关于如何检测非“GSM 7位字母表”输入字段中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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