Image type validation using Javascript
Please share your comments and feedback.Thanks.Please subscribe my updates via email.
JQuery Function
<script type="text/javascript">
function Validate() {
var img = document.getElementById('image');
var Path = img.value;
if (Path == '') {
alert("Please upload an image");
} else {
var Extension = Path.substring(
Path.lastIndexOf('.') + 1).toLowerCase();
if (Extension == "gif" || Extension == "png" || Extension == "bmp"
|| Extension == "jpeg" || Extension == "jpg") {
if (img.files && img.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(img.files[0]);
}
}
else {
alert("Upload only GIF, PNG, JPG, JPEG and BMP images.");
}
}
}
</script>
function Validate() {
var img = document.getElementById('image');
var Path = img.value;
if (Path == '') {
alert("Please upload an image");
} else {
var Extension = Path.substring(
Path.lastIndexOf('.') + 1).toLowerCase();
if (Extension == "gif" || Extension == "png" || Extension == "bmp"
|| Extension == "jpeg" || Extension == "jpg") {
if (img.files && img.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(img.files[0]);
}
}
else {
alert("Upload only GIF, PNG, JPG, JPEG and BMP images.");
}
}
}
</script>
HTML
<input type="file" name="imagename" id="image" onchange="return Validate()" />
No comments:
Post a Comment