Skip to content

Commit

Permalink
Merge pull request #1 from ahbanavi/master
Browse files Browse the repository at this point in the history
Add New Validation Rule (Persian Alpha Eng Num)
  • Loading branch information
sadegh19b authored Nov 20, 2020
2 parents b9ba2fa + ec8143a commit f666f85
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions lang/en/persian-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'persian_alpha' => 'The :attribute must be a persian alphabet.',
'persian_num' => 'The :attribute must be a persian number.',
'persian_alpha_num' => 'The :attribute must be a persian alphabet or number.',
'persian_alpha_eng_num' => 'The :attribute must be a persian alphabet or number or english number.',
'persian_not_accept' => 'The :attribute could not be contain persian alphabet or number.',
'ir_mobile' => 'The :attribute must be a iranian mobile number.',
'ir_phone' => 'The :attribute must be a iranian phone number.',
Expand Down
1 change: 1 addition & 0 deletions lang/fa/persian-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'persian_alpha' => ':attribute فقط میتواند شامل حروف فارسی باشد.',
'persian_num' => ':attribute فقط میتواند شامل اعداد فارسی باشد.',
'persian_alpha_num' => ':attribute فقط میتواند شامل حروف و اعداد فارسی باشد.',
'persian_alpha_eng_num' => ':attribute فقط میتواند شامل حروف و اعداد فارسی و اعداد انگلیسی باشد.',
'persian_not_accept' => ':attribute فقط میتواند شامل حروف یا اعداد لاتین باشد.',
'ir_mobile' => $invalidMsg,
'ir_phone' => $invalidMsg,
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ You can access to validation rules by passing the rules key according blew follo
| persian_alpha | Persian alphabet | صادق
| persian_num | Persian numbers | ۱۲۳۴
| persian_alpha_num | Persian alphabet and numbers |صادق۱۲۳۴
| persian_alpha_eng_num | Persian alphabet and numbers with english numbers |صادق۱۲34
| persian_not_accept | Doesn't accept Persian alphabet and numbers | cant be persian
| ir_mobile | Iranian mobile numbers | 00989173456789, +989173456789, 989173456789, 09173456789, 91712345678
| ir_phone | Iranian phone numbers | 37236445
Expand Down Expand Up @@ -76,6 +77,17 @@ $rules = [ 'persian_alpha_num' ];
Validator::make( $input, $rules );
```

### Persian Alpha Eng Num
Validate Persian alpha num with english num:

```
$input = [ '۰فارسی۱۲۳۴۵6789' ];
$rules = [ 'persian_alpha_eng_num' ];
Validator::make( $input, $rules );
```

### Iran mobile phone
Validate Iranian mobile numbers (Irancell, Rightel, Hamrah-e-aval, ...):

Expand Down Expand Up @@ -152,6 +164,8 @@ Validator::make( $request->all(), [

'city' => 'persian_alpha_num|min:10', // Validate persian alphabet & numbers at least 10 digit accepted

'address' => 'persian_alpha_eng_num', // Validate persian alphabet & numbers with english numbers

'mobile' => 'ir_mobile', // Validate mobile number

'sheba_number' => 'ir_sheba', // Validate sheba number of bank account
Expand Down
1 change: 1 addition & 0 deletions src/PersianValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PersianValidationServiceProvider extends ServiceProvider
'persian_alpha' => 'PersianAlpha',
'persian_num' => 'PersianNumber',
'persian_alpha_num' => 'PersianAlphaNumber',
'persian_alpha_eng_num' => 'PersianAlphaEngNumber',
'persian_not_accept' => 'PersianNotAccept',
'ir_mobile' => 'IranianMobile',
'ir_phone' => 'IranianPhone',
Expand Down
16 changes: 15 additions & 1 deletion src/PersianValidators.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public function validatePersianAlphaNumber($attribute, $value, $parameters)
return preg_match('/^[\x{600}-\x{6FF}\x{200c}\x{064b}\x{064d}\x{064c}\x{064e}\x{064f}\x{0650}\x{0651}\s]+$/u', $value);
}


/**
* Validate persian alphabet, persian number, english number and space.
*
* @param $attribute
* @param $value
* @param $parameters
* @return bool
*/
public function validatePersianAlphaEngNumber($attribute, $value, $parameters)
{
return preg_match('/^[\x{600}-\x{6FF}\x{200c}\x{064b}\x{064d}\x{064c}\x{064e}\x{064f}\x{0650}\x{0651}\0-9\s]+$/u', $value);
}

/**
* Validate string that is not contain persian alphabet and number.
*
Expand Down Expand Up @@ -129,7 +143,7 @@ public function validateIranianPhoneWithAreaCode($attribute, $value, $parameters
*/
public function validateIranianPostalCode($attribute, $value, $parameters)
{
return preg_match("\b(?!(\d)\1{3})[13-9]{4}[1346-9]-?[013-9]{5}\b", $value);
return preg_match("/\b(?!(\d)\1{3})[13-9]{4}[1346-9]-?[013-9]{5}\b/", $value);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/PersianValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,41 @@ public function testPersianAlphaNumber()
$this->assertEquals(true, $this->persianValidator->validatePersianAlphaNumber($this->attribute, $this->value, $this->parameters));
}

/**
* Unit test of persian alphabet and number with english number
*
* @return void
*/
public function testPersianAlphaEngNumber()
{
$this->value = "Sadegh1234";
$this->assertEquals(false, $this->persianValidator->validatePersianAlphaEngNumber($this->attribute, $this->value, $this->parameters));

$this->value = "1111صادق";
$this->assertEquals(true, $this->persianValidator->validatePersianAlphaEngNumber($this->attribute, $this->value, $this->parameters));

$this->value = "1111صادق۱۲۳۴";
$this->assertEquals(true, $this->persianValidator->validatePersianAlphaEngNumber($this->attribute, $this->value, $this->parameters));

$this->value = "صادق";
$this->assertEquals(true, $this->persianValidator->validatePersianAlphaEngNumber($this->attribute, $this->value, $this->parameters));

$this->value = "۱۲۳۴";
$this->assertEquals(true, $this->persianValidator->validatePersianAlphaEngNumber($this->attribute, $this->value, $this->parameters));

$this->value = "1234";
$this->assertEquals(true, $this->persianValidator->validatePersianAlphaEngNumber($this->attribute, $this->value, $this->parameters));

$this->value = "Sadegh۱۲۳۴صادق";
$this->assertEquals(false, $this->persianValidator->validatePersianAlphaEngNumber($this->attribute, $this->value, $this->parameters));

$this->value = "۱۲۳۴ صادق";
$this->assertEquals(true, $this->persianValidator->validatePersianAlphaEngNumber($this->attribute, $this->value, $this->parameters));

$this->value = "وَحِیُدّ‌الٍمٌاًسی";
$this->assertEquals(true, $this->persianValidator->validatePersianAlphaEngNumber($this->attribute, $this->value, $this->parameters));
}

/**
* Unit test of iranian mobile number
*
Expand Down

0 comments on commit f666f85

Please sign in to comment.