t6n's avatar
Level 1

validate integer starting with 0

Hi! I would like to validate '03' to check if it is an integer. This validation should return true. I cannot use the numeric rule as I need the input to be a whole number.

'birth_date_day' => 'required|integer|between:1,31',

What's the best way to achieve this?

0 likes
8 replies
Codenator81's avatar

This can help. StackOverflow
So from there just create new validate func which check for string is integer or just convert it to integer
(int) "03"

pmall's avatar
pmall
Best Answer
Level 56

Regex validation rule ?

'birth_date_day' => 'required|regex:[0-9]+|between:1,31'
pmall's avatar

Yes so I check if this string looks like an integer with leading zeros ;)

1 like
canfone's avatar

I am having this error

preg_match(): Unknown modifier '+'

When I try using @pmall answer.

2 likes
Webforward's avatar

Sorry, I know this is an old thread but I found this in google and none of the above solutions worked. I guess that if I found it, others will too.

@bryanscott suggestion expected the value to be between 1 and 31 characters long, not between 1 and 31 numerically. This may have worked on older versions.

The solution I found that worked in Laravel 8 was using the 'numeric' validation rule. This may have not existed when this post was started.

'dob_day' => 'required|numeric|between:1,31'

This works with values like 5 and 05, but expects the value to be between the intval of 1 and 31 numerically.

My full code:

'dob_day' => 'required|numeric|between:1,31',

'dob_month' => 'required|numeric|between:1,12',

'dob_year' => 'required|numeric|between:1900,2022',

Please or to participate in this conversation.