'Integer or String for a phone number?
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?
Solution 1:[1]
I think the validation should be like this
'phone' => 'required|numeric|min:8|max:11',
Solution 2:[2]
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
) 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'
Solution 3:[3]
You can also use
'phone' => ' required|digits:10'
Solution 4:[4]
You can try this one:
'phone' => 'required|min:8|max:11|regex:/^([0-9\s\-\+\(\)]*)$/',
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ashok poudel |
Solution 2 | |
Solution 3 | Khn Rzk |
Solution 4 | Hesam Moosapour |