Home » , » Validate Latitude and Longitude with regex and php

Validate Latitude and Longitude with regex and php

Written By Unknown on Jumat, 15 November 2013 | 10.22

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.

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";
}


Share this article :

Posting Komentar

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger