70

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
  • 100,514
  • 17
  • 155
  • 179
moujtahed
  • 721
  • 1
  • 5
  • 7

16 Answers16

74

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
  • 100,514
  • 17
  • 155
  • 179
  • 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
29

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
  • 713,503
  • 59
  • 514
  • 593
25

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ء-ي]

16

use this

[\u0600-\u06FF]

it worked for me on visual studio

enter image description here

Basheer AL-MOMANI
  • 13,431
  • 9
  • 89
  • 88
9

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

[گچپژیلفقهمو ء-ي]+$
Saman Sattari
  • 2,845
  • 5
  • 26
  • 38
7

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

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

Shir
  • 1,106
  • 12
  • 33
Mousa Ibrahim
  • 71
  • 1
  • 1
5

Simple, use this code:

^[؀-ۿ]+$

This works for Arabic/Persian even numbers.

Payam Yasaie
  • 51
  • 1
  • 3
2
function HasArabicCharacters(string text)

{

    var regex = new RegExp(

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

    return regex.test(text);
}
saghar.fadaei
  • 2,745
  • 1
  • 15
  • 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
  • 21
  • 2
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
  • 18,034
  • 23
  • 80
  • 97
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
  • 163
  • 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
  • 747
  • 10
  • 22
-1
[\p{IsArabic}-[\D]]

An Arabic character that is not a non-digit

Yusuf
  • 121
  • 8