How to Detect Non “GSM 7 bit alphabet” characters in input field

て烟熏妆下的殇ゞ 提交于 2019-11-27 06:11:18

问题


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

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>

回答1:


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;

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.




回答2:


function isGSMAlphabet(text) {
    var regexp = new RegExp("^[A-Za-z0-9 \\r\\n@£$¥èéùìòÇØøÅå\u0394_\u03A6\u0393\u039B\u03A9\u03A0\u03A8\u03A3\u0398\u039EÆæßÉ!\"#$%&'()*+,\\-./:;<=>?¡ÄÖÑܧ¿äöñüà^{}\\\\\\[~\\]|\u20AC]*$");

    return regexp.test(text);
}

This regular expression should solve your problem.




回答3:


Try this

http://www.frightanic.com/2012/04/10/regex-for-gsm-03-38-7bit-character-set/




回答4:


I have textarea with id smscontent. I use below regex/code

$('#smscontent').on('input, change keyup', function(){
    $(this).val($(this).val().replace(/[^A-Za-z0-9 \r\n@£$¥!\"#$%&amp;'\(\)*\+,_.\/:;&lt;=&gt;?^{}\\\[~\]]*/ig, ''));
});

To test the regex shared by Lajos - https://www.regextester.com/99623

To test the regex used in this answer - https://www.regextester.com/?fam=106436



来源:https://stackoverflow.com/questions/12673120/how-to-detect-non-gsm-7-bit-alphabet-characters-in-input-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!