82

I want Regular Expression to accept only Arabic characters, Spaces and Numbers.

Numbers are not required to be in Arabic.

I found the following expression:

^[\u0621-\u064A]+$

which accepts only only Arabic characters while I need Arabic characters, Spaces and Numbers.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
moujtahed
  • 841
  • 1
  • 7
  • 7

17 Answers17

88

Just add 1-9 (in Unicode format) to your character-class:

^[\u0621-\u064A0-9 ]+$

OR add \u0660-\u0669 to the character-class which is the range of Arabic numbers :

^[\u0621-\u064A\u0660-\u0669 ]+$
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • 1
    Thank you, your expression accept تجريب123 but doesn't accept تجريب 123 تجريب 123 could you please modify it to accept spaces too – moujtahed Apr 19 '15 at 12:19
  • @moujtahed you just need to add a space inside the brackets. – elixenide Apr 19 '15 at 12:28
  • I don't have any experience in Regular Expression formatting, can you please post an expression accept:Arabic characters, Spaces and Numbers – moujtahed Apr 19 '15 at 12:38
  • 1
    @moujtahed Welcome! as Ed Cottrell says just add a space in character class! check out the edit! – Mazdak Apr 19 '15 at 13:12
  • it works fine in flutter inputFormatters: [ FilteringTextInputFormatter.allow(RegExp("[\u0621-\u064A]+")), ], – ahmed Aug 13 '22 at 18:06
  • it works fine with white space ^[\u0621-\u064A\u0660-\u0669 ]+$ – ahmed Sep 04 '22 at 11:11
30

You can use:

^[\u0621-\u064A\s\p{N}]+$

\p{N} will match any unicode numeric digit.

To match only ASCII digit use:

^[\u0621-\u064A\s0-9]+$

EDIT: Better to use this regex:

^[\p{Arabic}\s\p{N}]+$

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
30

you can use [ء-ي] it worked for me in javascript Jquery forme.validate rules

for my example I want to force user to insert 3 characters

[a-zA-Zء-ي]

18

use this

[\u0600-\u06FF]

it worked for me on visual studio

enter image description here

Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
9

With a lot of try and edit i got this for Persian names:

[گچپژیلفقهمو ء-ي]+$
Saman Sattari
  • 3,322
  • 6
  • 30
  • 46
7

^[\u0621-\u064Aa-zA-Z\d\-_\s]+$

This regex must accept Arabic letters,English letters, spaces and numbers

Shir
  • 1,157
  • 13
  • 35
Mousa Ibrahim
  • 71
  • 1
  • 1
6

Simple, use this code:

^[؀-ۿ]+$

This works for Arabic/Persian even numbers.

Payam Yasaie
  • 61
  • 1
  • 3
3

This one allows Arabic letters, Arabic numbers and English numbers

var arabic = RegExp("^[\u0621-\u064A\u0660-\u0669 1-9]+\$");
skm
  • 559
  • 1
  • 6
  • 22
2
function HasArabicCharacters(string text)

{

    var regex = new RegExp(

        "[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]");

    return regex.test(text);
}
saghar.fadaei
  • 3,205
  • 1
  • 17
  • 10
  • 1
    Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 24 '17 at 11:44
2

To allow Arabic + English Letters with min&max allowed number of characters in a field, try this, tested 100%: ^[\u0621-\u064A\u0660-\u0669a-zA-Z\-_\s]{4,35}$ A- Arabic English letters Allowed. B- Numbers not allowed. C- {4,35} means the Min,Max characters allowed. Update: On submit: Accepted English words with spaces, but the Arabic words with spaces could not be submitted!

All cases tested

Ali Borsan
  • 41
  • 3
1

Regex for English and Arabic Numbers only

function HasArabicEnglishNumbers(text)

{

    var regex = new RegExp(

        "^[\u0621-\u064A0-9]|[\u0621-\u064A\u0660-\u0669]+$");

    return regex.test(text);
} 
1
@Pattern(regexp = "^[\\p{InArabic}\\s]+$")

Accept arabic digit and character

0

In PHP, use this:

preg_replace("/\p{Arabic}/u", 'x', 'abc123ابت');// will replace arabic letters with "x".

Note: For \p{Arabic} to match arabic letters, you need to pass u modifier (for unicode) at the end.

evilReiko
  • 19,501
  • 24
  • 86
  • 102
0

The posts above include much more than arabic (MSA) characters, it includes persian, urdu, quranic symbols, and some other symbols. The arabic MSA characters are only (see Arabic Unicode)

[\u0621-\u063A\u0641-\u0652] 
Halberdier
  • 1
  • 1
  • 1
0

I always use these to control user input in my apps

public static Regex IntegerString => new(@"^[\s\da-zA-Zء-ي]+[^\.]*$");
public static Regex String => new(@"^[\sa-zA-Zء-ي]*$");
public static Regex Email => new(@"^[\d\@\.a-z]*$");
public static Regex Phone => new(@"^[\d\s\(\)\-\+]+[^\.]*$");
public static Regex Address => new(@"^[\s\d\.\,\،\-a-zA-Zء-ي]*$");
public static Regex Integer => new(@"^[\d]+[^\.]*$");
public static Regex Double => new(@"^[\d\.]*$");
Ahmed Alayat
  • 172
  • 1
  • 1
  • 11
0

This is useful example

public class Test {

public static void main(String[] args) {
    String thai = "1ประเทศไทย1ประเทศไทย";
    String arabic = "1عربي1عربي";

    //correct inputs
    System.out.println(thai.matches("[[0-9]*\\p{In" + Character.UnicodeBlock.THAI.toString() + "}*]*"));
    System.out.println(arabic.matches("[[0-9]*\\p{In" + Character.UnicodeBlock.ARABIC.toString() + "}*]*"));

    //incorrect inputs
    System.out.println(arabic.matches("[[0-9]*\\p{In" + Character.UnicodeBlock.THAI.toString() + "}*]*"));
    System.out.println(thai.matches("[[0-9]*\\p{In" + Character.UnicodeBlock.ARABIC.toString() + "}*]*"));
    
}

}

Hazim
  • 805
  • 11
  • 24
-1
[\p{IsArabic}-[\D]]

An Arabic character that is not a non-digit

Yusuf
  • 140
  • 11