Alphanumeric validation error when using German

In an application intended for Germany , I write several web services . I have to apply some validation rules for username, password, etc., e.g. alphanumeric , special charecters are not supported, etc. I used regex to achieve this, but it fails, it works great for English, but it does not support German , how can I deal with this problem.

Any help would be appreciated ... preg_match("#.*^(?=.{8,20})(?=.*[a-zA-Z])(?=.*[0-9]).*$#", $string)

+4
source share
1 answer

First use the 'u' modifier for UTF-8 (the encoding of which you should use for international applications). Secondly, if you are trying to resolve umlauts, etc. In the [a-zA-Z] block, you can add the specific characters you want using escape sequences as follows:

separately:

 preg_match( "/\x{00FC}/u" , 'ΓΌ' ); // 1 

or in a group:

 preg_match('/^[\x{00DF}\x{00E4}\x{00C4}\x{00F6}\x{00D6}\x{00FC}\x{00DC}]+$/u', 'ΓŸΓ€Γ„ΓΆΓ–ΓΌΓœ'); // 1 

... or just use the literal matching \w (noting that it will also allow the use of numbers and underscores and other international characters that can serve as letters).

 preg_match( "/\w/u" , 'ΓΌ' ); // 1 

(I'm not sure what you are doing with ^ in the middle of the expression, btw, without the 'm' modifier.)

+1
source

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


All Articles