RegExp Confirm SMS text

How to write RegExp to check SMS text is only a keyboard character (abc, ABC, 123, ~! @ # $% ^ & * () `[] {} |; ':',. / <;>?)

Thank...

+3
source share
4 answers

The default GSM character set is defined in GSM 03.38 . Assuming you are looking at decoded text and not at the 7-bit packed format that is actually used, a regular expression like the one below should limit you to valid characters

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

Please note that you can send Unicode UCS-2 messages, after which the phone receiving the message must have suitable glyphs for presentation to the user, Unicode itself is not a limiting factor.

+8

.

, [ ] ( ) , ( coud )

^[a-zA-Z0-9~!@#$%^&*()`\[\]{};':,./<>?| ]*$

, . , - _ SMS.

+4

, , , .

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

, , . Twitter:

https://github.com/twitter/cloudhopper-commons-charset

It provides a great way to clear strings before sending them based on encodings. It also supports byte-based string encoding based on friendly SMS encoding. Here is my example of clearing an existing string before sending via SMS using their libraries:

public static String cleanSMS(String msg) {
    Charset charset = CharsetUtil.map(CharsetUtil.NAME_GSM7);
    StringBuilder cleaned  = new StringBuilder(msg);
    log.info("Accent chars replaced: " + MobileTextUtil.replaceAccentedChars(cleaned));
    log.info("Safe chars replaced: " + MobileTextUtil.replaceSafeUnicodeChars(cleaned));
    return CharsetUtil.normalize(cleaned.toString(), charset);
}
0
source

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


All Articles