Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

javascript - How to Detect Non "GSM 7 bit alphabet" characters in input field

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
function isGSMAlphabet(text) {
    var regexp = new RegExp("^[A-Za-z0-9 \r\n@£$¥èéùìò?????u0394_u03A6u0393u039Bu03A9u03A0u03A8u03A3u0398u039E???é!"#$%&'()*+,\-./:;<=>?????ü§????üà^{}\\\[~\]|u20AC]*$");

    return regexp.test(text);
}

This regular expression should solve your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...