我正在尝试使用Yii2中的正则表达式验证器来验证以下电话号码:
+6018-2031218
0060182031218
006018-2031218
像这样
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}/'],
];
}
但我无法得到正确的正则表达式来匹配这些电话号码,有没有其他不同的方法可以在Yii2中实现。
^(?:00|\+)[0-9]{4}-?[0-9]{7}$
。 - revo