Validate Filename Before Uploading Image Using JavaScript

Today’s helpful snippet will help you validate the filename of an image that’s being uploaded onto the server using JavaScript, making sure that it doesn’t consist of any unwanted spaces or invalid characters. This basically prevents the user from uploading or submitting any image file unless the proper format is used.

<script language=”javascript">
function Checkfiles()
{
 var fup = document.getElementById('filename');
 var fileName = fup.value;
 var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
 if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG" || ext == "doc")
 {
 return true;
 }
 else
 {
 alert("Upload Gif or Jpg images only");
 fup.focus();
 return false;
 }
}
</script>
<li>
 <div>
 <input type="file" id="logo1" onchange="Checkfiles()" multiple accept='image/*' name="txtLogoFileName" tabindex="20" value="<?php if($_POST["txtLogoFileName"]) echo $_POST["txtLogoFileName"]; else echo($LogofileName); ?>" />
 </div>
</li>   

Incoming search terms for the article:

Related Posts

Creating a Psychedelic Art Effect in Your Portraits

Colorize Your Source Code With Sunlight

Inspirational Photo Retouches By Cristian Girotto

Create Your Own Sticker Design Via Photoshop