Skip to main content


Showing posts from April, 2011

JS function to generate random number or random string

Javascript function to generate random number

Javascript random number generation function is Math.random()

alert(Math.random());// returns number like 0.3871769046200184

The above JavaScript statement returns a random number between 0 and 1


var randnum=Math.floor(Math.random()*1001);

This will generate a random number between 0 and 1001

Thumbnail creation on runtime php function

Function to create thumbnails of the images. this create thumbnails on runtime and will be displayed on browser. it accepts jpg,gif and png. the function is extendable. The function will generate thumbails of the images in the server or images from other domains.

Save the following code as thumbimage.php

the thumbnail generator is triggered by calling the page as thumbimage.php?src=sample.png

where src is the image file name. In this example the src is passed as urlencoded string.

JS function to check the extension of file in the url

Javascript function to check the extension of file in the url. This is useful for setting filters for file control. the function and it sample use is given below:

function checkFileType(str){
file_name=str.replace(/^.*(\\|\/|\:)/, '');
if( ext!= 'doc' && ext!= 'jpg' && ext!= 'jpeg' && ext!= 'png' && ext!= 'gif' && ext!= 'pdf'){
alert("Selected file "+file_name+" is invalid, Please upload file of type doc,jpg,jpeg,pdf,gif or png. ");

Sample usage

<input id="file" name="file" onchange="javascript:checkFileType(this.value);" type="file" />

Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to   Read more »
Search This Blog