0

I create request in laravel for validation where i have to validate phone number array.How to validate it. Below i am sharing:

Array that i pass in postman is:
    {
        "name": "test nbame",
        "phone_number": {
            "number": "+8896 5432",
            "internationalNumber": "+852 7896 5432",
            "nationalNumber": "+8896 5432",
            "e164Number": "+85278965432",
            "countryCode": "HK",
            "dialCode": "+852"
        },
        "designation": "xyz"
    }
   
public function rules()
        {
            return [
                'name'           => 'required',
                'dial_number'    =>  'required',
                'phone_number'   => 'required|regex:/(^[0-9 ]+$)+/',
                //'image'           =>  'required',
            ];
        }
    

how to validate phone_number array.

1

3 Answers 3

1

Based on your json you can do something like below

public function rules()
{
    return [
        'name'           => 'required',
        'dial_number'    =>  'required',
        'phone_number'   => 'required|array',
        'phone_number.number' => 'required|regex:/(^[0-9 ]+$)+/',
        'phone_number.internationalNumber' => 'required|regex:/(^[0-9 ]+$)+/',
        'phone_number.nationalNumber' => 'required|regex:/(^[0-9 ]+$)+/',
        'phone_number.e164Number' => 'required',
        'phone_number.countryCode' => 'required|string',
        'phone_number.countryCode' => 'required|string',
        'phone_number.dialCode' => 'required',
        //'image'           =>  'required',
    ];
}

for more info you can visit laravel documentation

Sign up to request clarification or add additional context in comments.

Comments

0

You're validating against a nested array in Laravel:

public function rules()
        {
            return [
                ...
                'phone_number.number'   => 'required|regex:/(^[0-9 ]+$)+/',
            ];
        }

Read more in the docs.

2 Comments

no t working this one. in postman i pass phone number array how to validate national number and dialcode
Refer to the docs I posted. You should find your answer.
0

You will check the number by regex. You will find a lot of variants of possible phone numbers. That means that you need a regex which is very flexible. How flexible the regex should be depends on you. here is an example:

'phone_number.number' => 'required|regex:/(^[0-9 ]+$)+/',

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.