RSS

Monthly Archives: December 2011

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].

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

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

Styling checkbox, radio button and select box

In a project i need to styling the checkbox, radio button and select button. I am not so strong in design and html :). Then searching in google and found a great site. I like it and works great in cross browser. The url is

http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/

 
Leave a comment

Posted by on December 29, 2011 in styling

 

Tags: ,