Click to See Complete Forum and Search --> : Checking is the file of a file button is a jpg or gif.
I have an image upload form.. i want to be able to check if the file the person has chosen is a gif or jpg and if not alert them of the error.
I know this can be done using string manipulation which i'm open to those suggestions as well, but is there a better way to make 100% sure the file is a gif or jpg?
Thank you!:D
AdamBrill
05-14-2003, 09:08 PM
I would do it something like this:<html>
<head>
<script language=javascript>
function go(formName){
ext = formName.fileInputName.value.substring(formName.fileInputName.value.length-4,formName.fileInputName.value.length);
ext = ext.toLowerCase();
if(ext == ".gif" || ext == ".jpg"){
return true;
}else{
alert("That is not a valid extension!");
return false;
}
}
</script>
</head>
<body>
<form onsubmit="return go(this);">
<input type=file name=fileInputName>
<input type=submit>
</form>
</body>
</html>
AdamBrill
05-14-2003, 09:33 PM
Ah, good point Dave. A little brain lapse on my part. ;) Anyway, you can also add more extensions into this line:
if(ext == ".gif" || ext == ".jpg"){
such as .jpeg, etc...
Thanks alot! Actually i went with the split function to create an array and get the extension that way and compare its value. This also insures a period was used which gives me a tad more assurence.
I really wanted to get the file type, which i can do with PHP but i wanted to do it javascript, but i guess this will do. :D