55

How can I define a regular expression which solely accept Persian alphabet characters?

I tried the following function, but it doesn't work properly:

function Just_persian(str){
  var p=/[پچجحخهعغفقثصضشسیبلاتنمکگوئدذرزطظژؤإأءًٌٍَُِّ\s]+$/;
  if(!str.match(p))
    alert("invalid format");
}
Ahmad
  • 5,551
  • 8
  • 41
  • 57
Angel
  • 733
  • 1
  • 7
  • 12
  • @BhavikKama: I didn't write any thing. it's my question that how can I do it. – Angel Jun 26 '13 at 09:16
  • stack-overflow is about finding the solution after you have tried something or googled..have you tried to search anything on that? – BhavikKama Jun 26 '13 at 09:17
  • @BhavikKama: yes, very much! but I didn't find my answer. help me please – Angel Jun 26 '13 at 09:18
  • @BhavikKama: excuse me, I am beginner in stack-overflow . why my question rated to -2 ???? – Angel Jun 26 '13 at 09:25
  • because in stackoverflow spoon feeding is not allowed..just ask the question with what is your question what have you tried yet and what are the steps you have taken to achieve the solution of your problem.just edit your question with the above mentioned details.thanks – BhavikKama Jun 26 '13 at 09:28
  • @BhavikKama: thank you very much, I didn't know these details. – Angel Jun 26 '13 at 09:59

3 Answers3

85

Persian characters are within the Arabic Unicode block, which ranges from U+0600 to U+06FF (which is specified in character class as \u0600-\u06FF).

function just_persian(str){
    var p = /^[\u0600-\u06FF\s]+$/;

    if (!p.test(str)) {
        alert("not format");
    }
}

Adapted to JavaScript from this question: Regex for check the input string is just in persian language

Community
  • 1
  • 1
BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • So you solved your problem?? – BhavikKama Jul 09 '13 at 05:17
  • 2
    The answer is utterly wrong. What's up with the `@"..."`? They won't just magically work when you copy from .NET code to JS. – nhahtdh Jul 20 '15 at 10:00
  • @nhahtdh gracefully fixed your own question without downvoting or presenting his own answer. Props to him. – Neil Jul 20 '15 at 13:13
  • @nhahtdh am developer of objective c..i just put ma logic. @ was prefix added in string when you writting in Objective c.. and i just give her a logical answer syntax and all should not matter – BhavikKama Jul 21 '15 at 07:42
  • @BhavikKama i have try with your code but his alwys getting alert so can you please explain in this funcation is getting automatically translate input value or enter directly persian lang. – coderwill Apr 29 '17 at 08:27
  • @BhavikKama can you please help me one? – coderwill Apr 29 '17 at 09:01
  • 1
    Please check this answer: https://stackoverflow.com/a/50018691/7330316 about Unicode of Persian characters. – Shahriar Jul 02 '21 at 13:46
  • Persian characters are NOT within the Arabic Unicode block. There are letters that don't exist in the Arabic language at all. – Makan Dec 22 '21 at 22:02
7

You can use persianRex, it detects all Persian characters in different keyboard layouts and it's open source.

Download it and put it in your project folder. Then include it in your HTML like this:

<script src="persian-rex/dist/persian-rex.js"></script>

Then in your Javascript you can do this:

function Just_persian(str){
  if(persianRex.text.test(str))
    alert("not format");
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Iman Mohamadi
  • 6,552
  • 3
  • 34
  • 33
5

Persian Characters are within the range: [\u0600-\u06FF] And: [\s]

Use This Code:

function Just_persian(str){
  var p=/@"^([\u0600-\u06FF]+\s?)+$"/;
  if(!str.match(p))
    alert("not format");
}

This Patern includes Letter and space Charachters.

AAKousha
  • 59
  • 1
  • 1