3

I'm trying to validate the following phone numbers using Regex validator in yii2:

+6018-2031218
0060182031218
006018-2031218

like this

 public function rules()
{
    return [
        [['name', 'minimum_order_amount', 'time_order_open', 'time_order_close', 'delivery_fee', 'rank', 'halal', 'featured', 'working_opening_hours', 'working_closing_hours', 'disable_ordering', 'delivery_duration', 'phone_number', 'longitude', 'latitude', 'image', 'status', 'owner_id', 'user_id'], 'required'],
        [['minimum_order_amount', 'delivery_fee', 'rank', 'longitude', 'latitude'], 'number'],
        [['time_order_open', 'time_order_close', 'working_opening_hours', 'working_closing_hours', 'created_at', 'updated_at'], 'safe'],
        [['halal', 'featured', 'disable_ordering', 'delivery_duration', 'status', 'owner_id', 'user_id'], 'integer'],
        [['name', 'phone_number', 'image'], 'string', 'max' => 255],
        [['working_opening_hours','working_closing_hours','time_order_open', 'time_order_close'], 'date', 'format' => 'H:m:s'],
        [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
        [['owner_id'], 'exist', 'skipOnError' => true, 'targetClass' => Owners::className(), 'targetAttribute' => ['owner_id' => 'id']],
        [['phone_number'], 'match', 'pattern' => '/((\+[0-9]{6})|0)[-]?[0-9]{7}/'],
    ];
}

but i can't get the right regex for those phones numbers is there any different way can we do this in yii2.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Fadi
  • 2,320
  • 8
  • 38
  • 77

1 Answers1

0

You can use this yii2 extension right here Yii2 International telephone numbers - Asset Bundle, Behavior, Validator, Widget

Eddie
  • 133
  • 1
  • 7