RSS

Category Archives: javascript

Get Address by postcode(Google Geocoding API)

Sometimes we need the address against postcode or need postcode against address. Google geocoding API provides this way correctly.

Suppose you have a text box and a button

<input type=”text” name=”location” id=”location” />
<input type=”button” name=”show” value=”show address” id=”show-add” />

Then it can be done by following script. I used here geocode v3.

<script type=”javascript/text” >
$(‘#show-add’).click(function(){
var location = $(‘#location’).val();
getAddress(location);
});

function getAddress(location){
geocoder = new google.maps.Geocoder();
geocoder.geocode( { ‘address’: location }, function(results, status) {
if (status == ‘OK’) {
alert(results[0].formatted_address);

} else {

alert(“Geocode was not successful for the following reason: ” + status);
}
});
}

</script>

for more informaton see this link : http://code.google.com/apis/maps/documentation/geocoding/

We can do this by v2 too but in v2 api_key is needed.

function getAddress(location){
$.getJSON(“http://maps.google.com/maps/geo?q=”+location+”key=”+googleApi+”&sensor=false&output=json&callback=?&#8221;,function(data, textStatus){
$.each(data.Placemark, function(key, val) {
alert(val.address);
});
});
}

 
Leave a comment

Posted by on January 17, 2012 in API, javascript, jQuery

 

Tags: , , ,

Regular expression for checking special characters in a string or file name

Today I have faced a problem that codeigniter cannot upload a file if the filename name contain special character. I solved the problem by validatiing the file name with javascript.

Suppose you have a form file elament like this

<input type=”file” name=”baseFile” id=”base-file” />

Then i get the file name by jQuery:

var filename = $(‘#base-file’).val();

and check by a function :

if(!checkSpecialCharacter(filename)){
alert(‘The file name may have special character. Please rename the file name and try to upload again.’);
return false;
}

The function is :

// check file name has special character
function checkSpecialCharacter(filename){
var re = new RegExp(“^[^<>%$#%@|]*$”);
var specialchars = re.test(filename);
return specialchars;
}

Note : if the function return false then the special character exist in the file name. You can add more special character in[^ special_character].

 
Leave a comment

Posted by on December 30, 2011 in javascript, jQuery

 

Tags: , ,

Get geographical location (geolocation) by IP address using jQuery

In my last post i wrote about how to get IP address in javascript. Now i am writing about how to get geoloaction by IP address. I have merged the code of previous post.

The code sample:

jQuery.getJSON(“http://jsonip.appspot.com?callback=?&#8221;,
function(data){
var clientip = data.ip;
var url =”http://freegeoip.net/json/”+clientip+&#8221;?callback=?”;
jQuery.getJSON(url, function(data){
$.each(data, function(key, val) {
alert(key+’ : ‘+val);
});
});
});

 
Leave a comment

Posted by on December 29, 2011 in javascript, jQuery

 

Tags: , , ,

Getting client IP address in Javascript

In javascript we can get the visitor IP address by following code:

jQuery.getJSON(“http://jsonip.appspot.com?callback=?&#8221;,
function(data){
alert(data.ip);

});

 
Leave a comment

Posted by on December 29, 2011 in javascript

 

Tags: ,

Unset an array item in javascript

Suppose you have an array my_array.

To unset an item of my_array just use the following:

my_array.splice((index),1);

where index is the key of the value of my_array which you want to unset.

 
Leave a comment

Posted by on December 29, 2011 in javascript

 

Tags: ,