CD..
CD..

Reputation: 74156

validate string - only specific language characters

Is there a way to check if a string contains characters of a given language only? (for example Japanese, Hebrew, Arabic)

I'm wondering if there is a way implementing this kind of validation in Javascript\Jquery and in c#?

EDIT

I'm not willing to check if the string contains valid words of a specific language dictionary. I'd like to validate that all characters belong to that language.

Upvotes: 3

Views: 3760

Answers (5)

amal50
amal50

Reputation: 1011

This is for Arabic text , but I didn't test it for other languages

^[\u0621-\u064A\040]+$

Upvotes: 1

m.benissa
m.benissa

Reputation: 661

internal bool HasArabicCharacters(string text)
{
    Regex regex = new Regex(
        "[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]");
    return regex.IsMatch(text);
} 

Upvotes: 0

Cleiton
Cleiton

Reputation: 18113

@CD, so sure you can do that.

In C#, just:

string str = "this text has arabic characters";
bool hasArabicCharacters = str.Any(c => c >= 0xFB50 && c <= 0xFEFC);

Upvotes: 4

Eldar Djafarov
Eldar Djafarov

Reputation: 24685

No, you cant check exact language. You can check only those chars which there no in other languages. For example cyriclics, hieroglyphs etc. Like a hint you can use google translate api to define in what lanuage user enters the text.

Upvotes: 0

eKek0
eKek0

Reputation: 23289

Maybe using a regular expression with UNICODE charset?

Upvotes: 0

Related Questions