Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating latitude/longitude in Laravel validation regex rule - preg_match(): No ending delimiter '/' found [duplicate]

I am trying to validate latitude/longitude in Laravel 5.4 project with custom regex rule (based on this answer: https://stackoverflow.com/a/22007205/2332336)

// 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?

like image 400
Latheesan Avatar asked Nov 08 '17 16:11

Latheesan


1 Answers

Is it possible that the pipes are the problem? See, for example, Laravel 5.4 - Validation with Regex. I see that your regexp has a pipe character in it. Try separating the rules into an array instead of using the pipe here: required|regex

In the second example, the first ^ is interpreted as the character that starts the regex string, so it expects a final ^ to match it.

like image 142
Martin Cook Avatar answered Oct 21 '22 06:10

Martin Cook