There are times when you need to validate latitude and longitude coordinates. May be you are taking it as user input or from some other source.
So here are two simple functions to validate latitude and longitude coordinates.
Regular expressions are used to match coordinates and php to check if it is valid or not.
So here are two simple functions to validate latitude and longitude coordinates.
Regular expressions are used to match coordinates and php to check if it is valid or not.
Function to validate latitude
function isValidLatitude($latitude){
if (preg_match("/^-?([1-8]?[1-9]|[1-9]0)\.{1}\d{1,6}$/", $latitude)) {
return true;
} else {
return false;
}
}
# Call function
if (isValidLatitude('28.6100')) {
echo "Valid Latitude";
}
Function to validate longitude
function isValidLongitude($longitude){
if(preg_match("/^-?([1]?[1-7][1-9]|[1]?[1-8][0]|[1-9]?[0-9])\.{1}\d{1,6}$/",
$longitude)) {
return true;
} else {
return false;
}
}
# Call function
if (isValidLongitude('77.2300')) {
echo "Valid Longitude";
}
Posting Komentar