Question

I am currently writing some validation that will validate inputted data. I am using regular expressions to do so, working with C#.

Password = @"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,18})$"

Validate Alpha Numeric = [^a-zA-Z0-9ñÑáÁéÉíÍóÓúÚüÜ¡¿{0}]

The above work fine on the latin alphabet, but how can I expand such to working with the Cyrillic alphabet?

Was it helpful?

Solution

The basic approach to covering ranges of characters using regular expressions is to construct an expression of the form [A-Za-z], where A is the first letter of the range, and Z is the last letter of the range.

The problem is, there is no such thing as "The" Cyrillic alphabet: the alphabet is slightly different depending on the language. If you would like to cover Russian version of the Cyrillic, use [А-Яа-я]. You would use a different range, say, for Serbian, because the last letter in their Cyrillic is Ш, not Я.

Another approach is to list all characters one-by-one. Simply find an authoritative reference for the alphabet that you want to put in a regexp, and put all characters for it into a pair of square brackets:

[АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя]

OTHER TIPS

You can use character classes if you need to allow characters of particular language or particular type:

@"\p{IsCyrillic}+" // Cyrillic letters
@"[\p{Ll}\p{Lt}]+" // any upper/lower case letters in any language

In your case maybe "not a whitespace" would be enough: @"[^\s]+" or maybe "word character (which includes numbers and underscores) - @"\w+".

Password = @"(?!^[0-9]*$)(?!^[А-Яа-я]*$)^([А-Яа-я0-9]{6,18})$"

Validate Alpha Numeric = [^а-яА-Я0-9ñÑáÁéÉíÍóÓúÚüÜ¡¿{0}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top