Monday, July 19, 2010

Validate Image Using Javascript

function validate_image()
{
frmobj = document.frmRegistration; //Here "frmRegistration" is Form name

var image_file = frmobj.upload_file.value; /*Here "upload_file" is in this tag like input type="file" name="upload_file" */

var image_length = frmobj.upload_file.value.length;

var pos = image_file.lastIndexOf('.') + 1;

var ext = image_file.substring(pos, image_length);

var final_ext = ext.toLowerCase();

if(final_ext != 'jpg' && final_ext != 'jpeg' && final_ext != 'gif' && final_ext != 'png')
{
alert('Upload valid image');
return false;
}
return true;
}
Usage:
if(!validate_image()) return false;

No comments:

Post a Comment