How to detect the characters "GSM 7 bit alphabet" in the input field

I am trying to determine if an input text field has any character that does not belong to the 7th GSM alphabet. The symbol table is here http://www.dreamfabric.com/sms/default_alphabet.html

After much 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 execute because it detects non-Latin characters. How can I change the regex to include the GSM 7 bitmap?

<!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> 
+6
source share
4 answers

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

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

Make sure you encode your encodings correctly, i.e. save the Javascript file as UTF8 and indicate that it is UTF8 for the browser .

+11
source
 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 regex should solve your problem.

+16
source

I have textarea with 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 check the regex provided by Lajos - https://www.regextester.com/99623

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

0
source

Source: https://habr.com/ru/post/926662/


All Articles