regexvalidationsms

RegExp Validate SMS Text


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

Thanks...


Solution

  • The default GSM character set is defined in GSM 03.38. Assuming you're looking at decoded text, not the 7bit packed format that is really used, a regex like the following should limit you to the allowable characters

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

    Note though that it is possible to sent unicode UCS-2 messages, at which point the handset receiving the message has to have suitable glyphs for presentation to the user, the unicode itself is not a limiting factor.