5

How to validate only numbers in laravel? I've try numeric and digits but it accept + and - character as a valid numbers / digits which I don't want. (+123 or -123 is validated as valid numeric)

Is there a existing rule on laravel that only accept 0-9 as a valid numbers or I have to go with regex instead?

3
  • You could do integer Commented Apr 15, 2016 at 2:58
  • @tam It's still accept + and - and cannot use 0 at front. (0123) is not valid Commented Apr 15, 2016 at 3:02
  • Oh right. Well ya I think u gotta use regex bc the PHP function is_numeric allows for the sign, it's a very simple regex though Commented Apr 15, 2016 at 3:13

2 Answers 2

15

So I end up using regex instead.

regex:/^[0-9]+$/
Sign up to request clarification or add additional context in comments.

Comments

2

try this rule :

digits_between:min,max

it only allows numbers, you just need to specify the min and max number of digits.
exemple:

'CowsNumber' => 'digits_between:3,10',

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.