8

I want to know if I have to use a string or an integer for a phone number?

I have tried an integer but I have a problem in my validation.

... table->integer('phone'); ...

In my validation, I must have between 8 and 11 characters.

I have tried this, but it doesn't work:

'phone' => 'required|numeric|between:8,11',

I think the string is better?

Kazmi
  • 1,198
  • 9
  • 20
user11124425
  • 961
  • 1
  • 11
  • 22
  • 4
    Possible duplicate of [What's the right way to represent phone numbers?](https://stackoverflow.com/questions/3483156/whats-the-right-way-to-represent-phone-numbers) (yes, that's a Java question, but the answer is the same: phone numbers are *always* strings) – IMSoP Aug 11 '19 at 09:35
  • It's better if you read here before making any assumption on phone numbers: https://github.com/jameslk/awesome-falsehoods and here: https://github.com/google/libphonenumber/blob/master/FALSEHOODS.md So, IMHO, use a string – dparoli Aug 11 '19 at 14:09

4 Answers4

9

“I want to know if I have to use a string or an integer for a phone number”

If you want to do some calculations with the numbers which you going to be storing (inserting), then you have to use int(in Laravel migration: it is integer) or any other data type such as double etc. (as per your requirement to do such operations) as the data type of the field.

However If you do not want to do some calculations with it, then use the data type as varchar (in Laravel migration: it is string) as the data type of the field.

So, when it comes to storing Phone numbers, you can use the varchar data type as you do not have to do calculations with Phone numbers.

So in this case, your validation should be this:

'phone' => 'required|string|min:8|max:11'
Kazmi
  • 1,198
  • 9
  • 20
  • +1 for this because in the future you may need to add the "+" symbol in your phone numbers or a "-" or another character. – padawanTony May 17 '22 at 17:31
2

I think the validation should be like this

'phone' => 'required|numeric|min:8|max:11',
ashok poudel
  • 703
  • 11
  • 28
2

You can also use

'phone' => ' required|digits:10'
Khn Rzk
  • 1,124
  • 2
  • 15
  • 26
0

You can try this one:

'phone' => 'required|min:8|max:11|regex:/^([0-9\s\-\+\(\)]*)$/',
Hesam Moosapour
  • 510
  • 5
  • 12