I have an input tel type field. That is for local mobile number with the format x-xxxx-xxx. With this, I am getting trouble invalidation. Even I enter eight digits it is giving digits validation error.
Validation Rule
I have tried multiple ways to validate as below but none of them works.
// without regex
'mobile' => 'nullable|digits:8',
// regex one
'mobile' => 'nullable|regex:/^([0-9\s\-\+\(\)]*)$/|digits:8',
// regex two
'mobile' => 'nullable|regex:/^\d{1}-\d{4}-\d{3}$/|digits:8',
Input Markup
I am using inputmask js plugin, in case if it requires.
<div class="form-group"><label for="mobile">{{__('admin.user.profile.mobile')}}</label>
<input type="tel" class="form-control" id="mobile" name="mobile"
value="{{ isset($user) ? $user->profile->mobile : old('mobile') }}"
placeholder="{{__('admin.user.profile.mobile')}}" data-inputmask="'mask': '9-9999-999'">
@error('mobile')
<span class="invalid-feedback d-block" role="alert"><strong>{{ $message }}</strong></span>
@enderror
</div>
Validation Error Message
The mobile must be 8 digits.
DD
So here I can understand the reason, is probably it is sending number with the format. So now how can I manage to validate it?
array:14 [▼
"_token" => "U5F3BkiAryYkaz75R1oraUfcx3ydz6bv6Ac7mw7K"
"_method" => "PUT"
"email" => "[email protected]"
"password" => null
"password_confirmation" => null
"role" => "super"
"first_name" => "Katheryn"
"last_name" => "Jones"
"mobile" => "4-6655-499"
"city" => "Taylorbury"
"facebook" => "http://fritsch.com/numquam-repudiandae-consectetur-sequi-suscipit-numquam"
"twitter" => "http://jacobi.com/"
"youtube" => "https://dubuque.org/explicabo-autem-corporis-distinctio.html"
"instagram" => "https://www.franecki.com/eos-non-nostrum-quia-commodi-ex-totam"
]
Question:
How to validate mobile numbers with fixed digits and format?
-(based on your regex) to the input. to digit be pass the validation should only contain number nothing else.digits:8then it should work. isn't it? Because all in one, I want to fix the number/digits. I don't want to allow the user to enter more than 8 digits.dd. I understand the issue. It is sending a number with the format. That is why not passing through validation as it is not only umber but the dashes as well. So, in this case, how can I force the total phone digits limit to 8 with the format?