Skip to content

Instantly share code, notes, and snippets.

@sivaprabug
Created February 28, 2022 06:55
Show Gist options
  • Save sivaprabug/0eef3dd53de3febd26f9b92afd261254 to your computer and use it in GitHub Desktop.
Save sivaprabug/0eef3dd53de3febd26f9b92afd261254 to your computer and use it in GitHub Desktop.
File extension Validation
<!DOCTYPE html>
<html>
<head>
<title>
File Type Validation while - Uploading it using JavaScript
</title>
<p>Upload an Image</p>
<input type="file" id="file" onchange="return fileValidation()" />
<div id="imagePreview"></div>
<script>
function fileValidation() {
var fileInput = document.getElementById('file');
var filePath = document.getElementById('file').value;
var filedetails = document.getElementById('file').files[0];
var allowedExtensions = /(\.iso|\.img)$/i;
if (!allowedExtensions.exec(filePath)) {
alert('Invalid file type');
fileInput.value = '';
return false;
} else {
alert('Valid file type');
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment