Dimslaev
Dimslaev

Reputation: 13

Jquery allow only Alpha Numeric + International characters (but not symbols)

I need to validate a form disallowing any symbols like ❦ ❧ ლ ۝.

I found this solution which works, but it also disallows international characters (e.g german characters - ü).

Ideally I should also be able to allow some punctuation - eg "-", ":".

        if( /[^a-zA-Z0-9\-\/]/.test($('.my_textarea').val()) ) {
            isFormValid = false;
        }

Upvotes: 1

Views: 1109

Answers (3)

collapsar
collapsar

Reputation: 17238

You may specify international characters in js regexen as individual unicode code points or ranges thereof using the \uXXXX syntax (XXXX representing the code point in hex). Therefore it might be a viable solution to add the code point ranges of alphabets you wish to support (it won't if you wish to support all scripts, of course, though you could apply different regexen depending on a country/language selection ).

Consult the unicode code chart synopsis or the wikipedia page (the pages for individual scripts contain the code point ranges in the summary box on the top right) to find the respective codes.

Example (cyrillic alphabet):

/[^a-zA-Z0-9\u0400-\u04ff\-\/]/

Upvotes: 1

KeyNone
KeyNone

Reputation: 9150

As it seems like you only have a small bunch of characters you want to allow, I would stick with whitelisting all allowed characters (instead of blacklisting the not allowed ones).
For example:

[^\w\-\/üäöÜÄÖ.,:;]

(\w is short for a-zA-Z0-9_, if you dont want the _, then stick with a-zA-Z0-9)

For further characters, just add them to your charactergroup.

Upvotes: 0

felipe_dmz
felipe_dmz

Reputation: 95

If you use POSIX expressions you'll allow any symbol and punctuation:

/[^a-zA-Z0-9\-\/[:ascii:][:punct:]]/

So, I think the best way to do that is to insert allowed symbols as exceptions in list:

/[^a-zA-Z0-9\-\/,.ü]/

Upvotes: 0

Related Questions