Skip to main content

Posts

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(/^.*(\\|\/|\:)/, ''); ext=file_name.split('.').pop().toLowerCase(); 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. "); document.getElementById('file').value=''; } } Sample usage <input id="file" name="file" onchange="javascript:checkFileType(this.value);" type="file" />

Dynamic control generation - Javascript code

The JS code for generating controls dynamically. Copy and paste the code and save it as an HTML file, take it on the browser to know the working of the script. The add Ctrl function will duplicate the TR of the table. On clicking the link "Add More Contact Persons" the new row of control will be created. On clicking the " Remove" link the corresponding row will be deleted.

JS function to convert string to US phone number format

Javascript function to change the string to US telephone number format Eg: if input string is 0115463234 ,it will be converted to (011)546-3234 function toPhoneFormat( obj ){ val=document.getElementById(obj).value; arrChars=val.split(''); formatedString=''; for(i=0;i < val.length;i++){ if(i==0) formatedString+='('; if(i==3) formatedString+=') '; if(i==6) formatedString+='-'; formatedString+=arrChars[i]; } document.getElementById(obj).value=formatedString; } Sample usage: Function is called in the onBlur event <input id="txtpersontel" type="text" name="persontel" maxlength="20" style="width:125px" onblur="toPhoneFormat('txtpersontel')" > Tweet

JS Function to check whether the string is numeric

Javascript function to check the string in the textbox is numeric function name : isNumber parameters : Input control object and flag to allow decimal points Function doesn't allow non numeric characters in the control the function can be called on onKeyUp event of the control. if permitDecimal = 0 then decimal point '.' is not allowed in string, else dot is allowed. function isNumber( obj ,permitDecimal ){ var val = document.getElementById(obj).value ; var te=val.match(/\d+\.?\d*/); document.getElementById(obj).value=te; val=te; if(val!=""){ if( isNaN(val) ){ endOFString=val.length-1; val=val.substring(0,endOFString); } if(!permitDecimal){ if(val){ val=val.toString(); document.getElementById(obj).value=val.replace(/\./g,''); } }else{ document.getElementById(obj).value=val; } } } Sample usage <input id="txtpersontel" type="text" name="persontel" maxlength="20" style=...


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