Created
February 28, 2022 06:55
-
-
Save sivaprabug/0eef3dd53de3febd26f9b92afd261254 to your computer and use it in GitHub Desktop.
File extension Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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