wfareed
wfareed

Reputation: 139

How can i allow only arabic characters in input text field?

I already searched here and found similar posts related to this post but i did not find a solution yet .

i tried this :

$text = "الحمد لله رب العالمين hello";

echo $is_arabic = preg_match('/\p{Arabic}/u', $text);

I add the unicode flag but if i add any English characters it is returning true ! any fix for this ?

Any idea folks ?

Thanks in advance

Upvotes: 2

Views: 4673

Answers (3)

salim fadhil
salim fadhil

Reputation: 1

I hope this will help you. to make the input accepts Arabic alphabets only you can try this.

<input type="text" oninput="this.value = this.value.replace(/[^ءآأ-ي ]/g, '').replace(/(\..*)(\s+)\..*/g, '$1');" />

Upvotes: 0

Toto
Toto

Reputation: 91518

Use unicode flag:

$text = "الحمد لله رب العالمين";
echo $is_arabic = preg_match('/\p{Arabic}/u', $text);
                                   here __^

If you want to match only arabic you should do:

echo $is_arabic = preg_match('/^[\s\p{Arabic}]+$/u', $text);

Upvotes: 5

Brett Zamir
Brett Zamir

Reputation: 14375

Update: I see I am apparently wrong about classes not being supported (though the docs at say "Extended properties such as "Greek" or "InMusicalSymbols" are not supported by PCRE" but the comment at http://php.net/manual/en/regexp.reference.unicode.php#102756 says they are supported), so I guess M42's is the better answer. They can, however, be done with ranges as follows:

$text = "الحمد لله رب العالمين";

echo $is_arabic = 
preg_match('/^[\s\x{0600}-\x{06FF}\x{0750}-\x{077F}\x{08A0}-\x{08FF}\x{FB50}-\x{FDFF}\x{FE70}-\x{FEFF}\x{10E60}\x{10E60}—\x{10E7F}\x{1EE00}—\x{1EEFF}]+$/u', $text);

Upvotes: 3

Related Questions