2

I am trying to validate the arabic and enligsh input field. I search in the stackoverflow and I found something but its not actually working. I want to validate the Full Name (which will have space), Mobile and title. Its working fine for english but not in arabic.

FULL NAME:

if(!preg_match('/^[a-zA-Z ]{6,60}$/', $sendername) && 
   !preg_match('/^[\u0600-\u06FF]{6,60}$/', $sendername) 
               )
{
}

MOBILE:

 if(!(preg_match('/^[0-9]{8,15}$/', $senderphone)) && 
    !(preg_match('/^[\u10E60—\u10E7F]{8,15}$/', $senderphone))
{
}

TITLE:

if( (strlen($sendersubject) < 10) || (strlen($sendersubject) > 100) )
{
}

Thanks dear for your support

  • Show us what you found, and why it is not working? – Cliff Burton Apr 21 '15 at 06:53
  • Dear I customize the code from this page http://stackoverflow.com/questions/11323596/regular-expression-for-arabic-language – Airlines Promotion Apr 21 '15 at 06:56
  • What's your question? BTW you shouldn't validate names: http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – Vsevolod Golovanov Apr 21 '15 at 07:03
  • They can write the names in arabic or english but not other character like )(*&&^ – Airlines Promotion Apr 21 '15 at 07:07
  • @AirlinesPromotion why the solution you provided is nit working? It is in `javascript` and, assuming you've not included `alert(arregex.test(text));` in your **PHP** script it should work. What I'm saying is: Show us what you've tried and show the errors you got too. – Cliff Burton Apr 21 '15 at 07:20
  • Anyway, in your `if()` condition you should use the **`OR`** operator, not the **`AND`** - `if(condition_1 || condition_2)`. Your `if()` will be executed only if neither *english* and *arabic* text is found. – Cliff Burton Apr 21 '15 at 07:39

2 Answers2

1

it will allow you to write only arabic letters and prevent special characters.

pass the string to below function.

function ValidateArabic($str) { 
    if (!preg_match("~^[a-z0-9٠-٩\-+,()/'\s\p{Arabic}]{1,60}$~iu", $str)) {
       return false;
    } else
      return true;
}

See Demo for arabic validation.

Noman
  • 4,088
  • 1
  • 21
  • 36
  • You've included arabic text in a php script? That is not a good solution!! – Cliff Burton Apr 21 '15 at 07:11
  • can you tell me why ? – Noman Apr 21 '15 at 07:16
  • Because the interpreter may not recognize this kind of character, a better solution could be including its character set instead. – Cliff Burton Apr 21 '15 at 07:27
  • Thank you for your suggestion i will be carefull next time. but this solution is working. – Noman Apr 21 '15 at 07:41
  • Glad to be of help. Are you arab / using an arab server? Because for me it is not working (two online servers and four local web-servers) – Cliff Burton Apr 21 '15 at 07:46
  • 1
    no its working on my local xamp server and also on our demos server. I use it in bornfire project. – Noman Apr 21 '15 at 07:48
  • I made this from your scirpt (!preg_match("~^[a-zA-Z\s, \s\p{Arabic}]{6,60}$~iu", $sendername) its working fine but I want to force user to have space. Could you please help ?? Also, for number I made this from your script !preg_match("~^[0-9٠-٩, \s\p{Arabic}]{9,15}$~iu", $errsenderphone) to enter arabic or english number. But its not accepting arabic number. please help. – Airlines Promotion Apr 21 '15 at 08:04
  • The problem with the number is its accepting arabic character also. could you please solve this. – Airlines Promotion Apr 21 '15 at 08:10
  • I manage the contact number. Please solve the full name issue as per my previous comment. – Airlines Promotion Apr 21 '15 at 08:24
  • `^[0-9٠-٩\-+,()/'\s\p{Arabic}]{1,60}$` use this regex for allow english or arabic numbers – Noman Apr 21 '15 at 09:51
  • or if you want check for number length then check `mb_strlen` function in php – Noman Apr 21 '15 at 09:53
  • Dear I am asking to force the user to enter space in their Full name. I use this (!preg_match("~^[a-zA-Z\s, \s\p{Arabic}]{6,60}$~iu", $sendername). But its accepting characters if ts more than 6 wihtout space also – Airlines Promotion Apr 21 '15 at 10:22
  • can you please provide example so i can easily understand what you actually want? – Noman Apr 21 '15 at 11:00
  • Full name must have space like we have first name, space and last name. if the user enter only name without space it means he didn't enter the full name. It may be first name of the last name. Like Mohammed Khan means its a full name. It should not accept either mohammed or khan. can we do like this ? – Airlines Promotion Apr 21 '15 at 11:08
  • 1
    this regex will work for full name `^[\w\\p{Arabic}]+\s[\w\\p{Arabic}]+$` – Noman Apr 21 '15 at 11:20
1

Could you not just use the whitespace expression in your regex match?

if(!preg_match('/^[a-zA-Z\s]{6,60}$/', $sendername) && 
   !preg_match('/^[\u0600-\u06FF]{6,60}$/', $sendername) 
               )
{
}

Some content and output would be useful however.

Cliff Burton
  • 3,414
  • 20
  • 33
  • 47
id0827502
  • 53
  • 8