RSS

Author Archives: mamoonblog

About mamoonblog

Try to discover new technology

Sorting formatted numbers in jquery datatable

We can sort formated numbers in jquery data table. Here is the code. If we initialize our jquery data table by class ‘datatable’ and the 5th column contains formatted numbers data( comma separated numbers) then we can use the following code.

$(‘.datatable’).dataTable( {
“aoColumns”: [
null,
null,
null,
null,
{ “sType”: “formatted-num” }
]
});

jQuery.fn.dataTableExt.oSort[‘formatted-num-asc’] = function(a,b) {
/* Remove any formatting */
var x = a.match(/\d/) ? a.replace( /[^\d\-\.]/g, “” ) : 0;
var y = b.match(/\d/) ? b.replace( /[^\d\-\.]/g, “” ) : 0;

/* Parse and return */
return parseFloat(x) – parseFloat(y);
};

jQuery.fn.dataTableExt.oSort[‘formatted-num-desc’] = function(a,b) {
var x = a.match(/\d/) ? a.replace( /[^\d\-\.]/g, “” ) : 0;
var y = b.match(/\d/) ? b.replace( /[^\d\-\.]/g, “” ) : 0;

return parseFloat(y) – parseFloat(x);
};

Advertisements
 
Leave a comment

Posted by on May 11, 2012 in jQuery, JS Plugins

 

Tags: , ,

A handy tutorial for .htaccess file

.htaccess files plays important role in the web technology. So its important to know how its works. This is a handy tutorial.

http://net.tutsplus.com/articles/news/htaccess-files-for-the-rest-of-us/

 

 
Leave a comment

Posted by on January 24, 2012 in Miscellaneous

 

Tags: ,

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: ,

Styling scroll bar

Today i need to styling the scroll bar like facebook and google. Then i start to search in google and found a cool jquery plugin. Works great in cross browser. The link is:

http://www.baijs.nl/tinyscrollbar/

 
Leave a comment

Posted by on December 29, 2011 in jQuery

 

Tags: ,