I am trying to validate latitude/longitude in Laravel 5.4 project with custom regex rule (based on this answer: https://mcmap.net/q/496928/-php-validate-latitude-longitude-strings-in-decimal-format)
// Create validation
$validator = Validator::make($request->all(), [
// ...
'lat' => 'required|regex:/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/',
'long' => 'required|regex:/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/'
], [
'lat.regex' => 'Latitude value appears to be incorrect format.',
'long.regex' => 'Longitude value appears to be incorrect format.'
]);
// Test validation
if ($validator->fails()) {
return redirect()
->back()
->withErrors($validator)
->withInput();
}
When this runs, I am getting the following error:
preg_match(): No ending delimiter '/' found
I've tried this also;
'lat' => 'required|regex:^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$',
'long' => 'required|regex:^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$'
Now I get this error:
preg_match(): No ending delimiter '^' found
What is the correct syntax to validate lat/lng in laravel via custom regex rule?