Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  129] [ 2]  / answers: 1 / hits: 29599  / 11 Years ago, mon, june 17, 2013, 12:00:00

I would like to allow all Alphanumeric Characters and disallow all Special Characters in a RegEx. But I would like to allow German Umlauts but becouse they are Special Chars too, I can't type them in. I use this Script:



    if(website_media_description.match(/[^a-zA-Z0-9]/g)) { 

alert('Found Special Char');

}


So when a äöüÄÖÜß is in the variable than I get the alert too. I also tryed this Script:



    if(website_media_description.match(/[^a-zA-Z0-9äöüÄÖÜß]/g)) { 

alert('Found Special Char');

}


But this also does not work. Can someone please tell me what I am doing wrong?



Thanks :)


More From » jquery

 Answers
12

my test String comes from an input field, i write description test 1 öäüÖÄÜß




Your problem is coming from the fact you haven't considered every character you want in your whitelist.



Let's consider what is actually matched by your test string



description test 1 öäüÖÄÜß.match(/[^a-zA-Z0-9äöüÄÖÜß]/g);
// [ , , ]


As we can see, it matched 3 times, and each time was whitespace. So, the solution is to add a space to your whitelist (assuming you don't want to allow tab/return etc).



description test 1 öäüÖÄÜß.match(/[^a-zA-Z0-9äöüÄÖÜß ]/g);
// null


Your test string now passes the RegExp without a match, which means it is valid in this case.


[#77579] Sunday, June 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
allanw

Total Points: 421
Total Questions: 132
Total Answers: 102

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
;