Laravel 5.6 - Validate Numbers and spaces


Laravel 5.6 - Validate Numbers and spaces



In Laravel 5.6 I am validating alphabetic characters and spaces only in regex like this..


'name' => 'required|regex:/^[pLs-]+$/u',



This works as far as I can see, I am now trying to validate numbers only and spaces like this..


'telephone' => 'required|regex:/[0-9 ]+/',



But this is not working and allows me to enter 'f4' where it should fail.



Where am I going wrong?





Try 'regex:/(^[0-9 ]+$)+/'
– Vincent Decaux
Jul 1 at 10:29


'regex:/(^[0-9 ]+$)+/'




1 Answer
1



[0-9 ]+ will match 4 in f4


[0-9 ]+


4


f4



Try using anchors to assert the start and the end of the line ^ and $


^


$



^[0-9 ]+$


^[0-9 ]+$



Note that this will also match whitespaces only because the character class matches digit or a whitespace one or more times..





Thank you, reading up on the whitespace now
– fightstarr20
Jul 1 at 10:47






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.

Popular posts from this blog

Boo (programming language)

How to make file upload 'Required' in Contact Form 7?