Laravel : size validation not working


Laravel : size validation not working



I am using trying to validate mobile number length.


$this->validate(request(),[
'mobile'=>'numeric|size:10|unique:client_details,contact',
]);



Still i am getting error: The mobile must be 10.
Is there any alternative method to validate.





try min and max
– Gaurav Gupta
Jun 29 at 7:39





Validate it as string. Mobile numbers are not generally always numeric
– apokryfos
Jun 29 at 8:06





2 Answers
2



As documents says:



The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.



So if you want to get mobile number then you should use string|size:10 and if you prefer to use numeric you should use max integer that you can get which is 9999999999.





btw i used this $this->validate(request(),[ 'mobile'=>'numeric|digits:10|unique:client_details,contact', ]); replaced size with digits and it works.
– Arun Verma
Jun 29 at 12:29



$this->validate(request(),[ 'mobile'=>'numeric|digits:10|unique:client_details,contact', ]);



Use laravel validation rule regex.


'phone' => 'numeric|unique:client_details,contact|regex:/[0-9]{10}/'



Laravel Docs



But also do note that mobile numbers sometimes need to be stored as string for example 022-123456 etc. for that you'll need to modify your regex little.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift