Skip to content
All posts
Laravel

Latitude and Longitude Validation In Laravel using Regex

March 28, 2023·Read on Medium·

When building web applications that require geolocation data, validating latitude and longitude inputs is a crucial step in ensuring accurate data entry. In this tutorial, we will explore how to validate latitude and longitude inputs in Laravel using regex.

Understanding Latitude and Longitude Coordinates

Latitude and longitude are used to specify a location on the Earth’s surface. Latitude measures how far north or south of the equator a location is, while longitude measures how far east or west of the Prime Meridian a location is.

Latitude and longitude coordinates are typically expressed in decimal degrees format, which is a decimal representation of degrees, minutes, and seconds.

For example, the latitude and longitude coordinates for the Eiffel Tower in Paris are:

Latitude: 48.8584° N 
Longitude: 2.2945° E

These coordinates can also be expressed in decimal degrees format as:

Latitude: 48.8584 
Longitude: 2.2945

In decimal degrees format, latitude ranges from -90.0 to 90.0, and longitude ranges from -180.0 to 180.0.

Why validate latitude and longitude?

Latitude and longitude are often used in web applications to display maps or to search for locations. If latitude and longitude coordinates are not properly validated, users could potentially enter invalid or malicious input that could cause errors or even security vulnerabilities in your application.

For example, a user might enter a latitude of 100 degrees, which is outside the valid range of -90 to 90 degrees. If this input is not properly validated, it could cause errors in your application or even allow an attacker to inject malicious input.

Validating latitude and longitude coordinates is therefore an important step in ensuring the security and reliability of your web application.

Validating using the regular expression

The regular expression we’ll be using to validate latitude and longitude coordinates is:

Latitude : /^[-]?((([0–8]?[0–9])(\.(\d{1,8}))?)|(90(\.0+)?))$/
Longitude : /^[-]?((((1[0–7][0–9])|([0–9]?[0–9]))(\.(\d{1,8}))?)|180(\.0+)?)$/

This regular expression matches latitude and longitude coordinates in decimal degree format, with the latitude ranging from -90 to 90 degrees and the longitude ranging from -180 to 180 degrees.

Let’s break down the regular expression into its individual components and explain what each one does.

/^[-]?((([0–8]?[0–9])(\.(\d{1,8}))?)|(90(\.0+)?))$/

This regular expression matches a valid latitude value in decimal degrees format which can be between -90 and 90.

Let’s break down the different components of this regular expression:

/^[-]?: This is the starting delimiter of the regular expression. It matches the start of the string and optionally matches a minus sign ("-").

(([0–8]?[0–9])(\.(\d{1,8}))?): This is the main body of the regular expression which matches the actual latitude or longitude value. It consists of two parts:

  1. ([0–8]?[0–9]): This matches the degrees part of the value which can be any number from 0 to 90. The first digit can be optional (hence the [0-8]?), so that numbers from 0 to 9 and 00 to 89 can be matched.
  2. (\.(\d{1,8}))?: This matches the decimal part of the value which is optional. It consists of a period (dot) followed by one to eight digits.

|(90(\.0+)?)): This is an alternative option to match the special value of 90 degrees. It consists of:

  1. 90: This matches the exact value of 90 degrees.
  2. (\.0+)?: This matches the optional decimal part which must consist of a period followed by one or more zeros.

$/: This is the end delimiter of the regular expression. It matches the end of the string.

/^[-]?((((1[0–7][0–9])|([0–9]?[0–9]))(\.(\d{1,8}))?)|180(\.0+)?)$/

This regular expression matches a valid longitude value in decimal degrees format which can be between -180 and 180.

Let’s break down the different components of this regular expression:

/^[-]?: This is the starting delimiter of the regular expression. It matches the start of the string and optionally matches a minus sign ("-").

((((1[0–7][0–9])|([0–9]?[0–9]))(\.(\d{1,8}))?)|180(\.0+)?): This is the main body of the regular expression which matches the actual longitude value. It consists of two parts:

  1. ((1[0–7][0–9])|([0–9]?[0–9])): This matches the degrees part of the value which can be any number from 0 to 180. The first digit can be optional (hence the [0-9]?), so that numbers from 0 to 9 and 00 to 99 can be matched. If the degrees part is between 100 and 179, the first digit must be 1 and the second digit can be any number from 0 to 9. This ensures that the longitude value is within the range of -180 to 180.
  2. (\.(\d{1,8}))?: This matches the decimal part of the value which is optional. It consists of a period (dot) followed by one to eight digits.

|180(\.0+)?: This is an alternative option to match the special value of 180 degrees. It consists of:

  1. 180: This matches the exact value of 180 degrees.
  2. (\.0+)?: This matches the optional decimal part which must consist of a period followed by one or more zeros.

$/: This is the end delimiter of the regular expression. It matches the end of the string.

Implement Validation Rule In Laravel

Now, for implementing this regex as a rule in Laravel, you can create a new custom rule by extending the Illuminate\Validation\Rule class. Here is an example implementation:

Create Rule for Latitude Validation named “LatitudeRule” and use PHP method preg_match to validate current value against the regex

use Illuminate\Validation\Rule;

class LatitudeRule extends Rule
{
public function passes($attribute, $value)
{
$regex = '/^[-]?((([0-8]?[0-9])(\.(\d{1,8}))?)|(90(\.0+)?))$/';
return preg_match($regex, $value);
}

public function message()
{
return 'The :attribute must be a valid latitude coordinate in decimal degrees format.';
}
}

Now, create Rule for Longitude Validation named “LongitudeRule” and use PHP method preg_match to validate current value against the regex

use Illuminate\Validation\Rule;

class LongitudeRule extends Rule
{
public function passes($attribute, $value)
{
$regex = '/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))(\.(\d{1,8}))?)|180(\.0+)?)$/';
return preg_match($regex, $value);
}

public function message()
{
return 'The :attribute must be a valid longitude coordinate in decimal degrees format.';
}
}

Both custom validation rule uses the regular expression to validate the input value in the passes method. If the input value matches the regular expression, the validation rule passes. If the input value does not match the regular expression, the validation rule fails and returns an error message in the message method.

To use this custom validation rule in your Laravel application, you can add it to your validation rules like this to your Controller:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CoordinateController extends Controller
{
public function create(Request $request)
{
Validator::validate($request->all(), [
'latitude' => ['required', new LatitudeRule],
'longitude' => ['required', new LongitudeRule],
]);

// Handle validation success
}
}

Above example shows it will ensure that the latitude and longitude fields in the request are both validated against the regular expression.

Conclusion

Validating latitude and longitude coordinates in decimal degrees format is an important step when working with geographic data. In Laravel, you can use regular expressions to ensure that these coordinates are in the correct format. By creating a custom validation rule and using the regular expression to validate the input value, you can easily add latitude and longitude validation to your Laravel application.

Found this helpful?

If this article saved you time or solved a problem, consider supporting — it helps keep the writing going.

Originally published on Medium.

View on Medium
Latitude and Longitude Validation In Laravel using Regex — Hafiq Iqmal — Hafiq Iqmal