Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for address field in Laravel

Hello I need to validate addresses like the following

C/s-2, abc, pqr
C/s/2, abc, pqr
221B Baker Street
101, Baker Street, London, UK
221-B, Baker Street etc

basically looking for standard address types that I can validate using laravel's validation

I have tried following expressions

'address' => 'required|regex:/^(?=.*[0-9])[- ,\/0-9]+$/',
'address' => 'required|regex:/[^a-zA-Z0-9\/-]|[,\/]$/s',
'address' => 'required|regex:/(^([0-9]+ )?[a-zA-Z ]+$)/',
'address' => 'required|regex:/(^[A-Za-z0-9 ]+$)/',

but nothing has worked for me can you please help to get the right regex for such addresses

Thank you :)

like image 798
Mr. Pyramid Avatar asked Oct 24 '25 07:10

Mr. Pyramid


1 Answers

finally got something that is working for me I don't know whether it will work for long or not but presently it is working well.

'address' => 'required|regex:/(^[-0-9A-Za-z.,\/ ]+$)/'

however the previous posted answer is also working well I don't know why he deleted that answer anyway I'm pasting his demo which worked for me

'address' => 'required|regex:/([- ,\/0-9a-zA-Z]+)/',

WORKING DEMO

like image 119
Mr. Pyramid Avatar answered Oct 26 '25 21:10

Mr. Pyramid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!