simple JS file type check by extension [solved]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

simple JS file type check by extension [solved]

Post by s.dot »

So this piece of code is inside of one of my functions. I just want to make sure that the file being uploaded ends in .jpg, .jpeg, .gif, .png... before I do server side validation of course. It's just a little convenience to the end-user.

snippet:

Code: Select all


var docform = document.forms[0];

if(
	!docform.ep_image.value.toLowerCase.match(/.+\.jpg$/) &&
	!docform.ep_image.value.toLowerCase.match(/.+\.jpeg$/) &&
	!docform.ep_image.value.toLowerCase.match(/.+\.gif$/) &&
	!docform.ep_image.value.toLowerCase.match(/.+\.png$/)
)
{
	alert('Invalid Image Type.  Valid file types are: JPEG, JPG, PNG, GIF');
	docform.submit.disabled = false;
	docform.submit.value = 'Upload Image';
	return false;
}
I get the error: "Object doesn't support this property or method".
Last edited by s.dot on Fri Mar 09, 2007 4:37 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

var imgvalue = docform.ep_image.value.toLowerCase();
if(
	!imgvalue.match(/^.+jpg$/) &&
	!imgvalue.match(/^.+jpeg$/) &&
	!imgvalue.match(/^.+gif$/) &&
	!imgvalue.match(/^.+png$/)
)
{
	alert('Invalid Image Type.  Valid file types are: JPEG, JPG, PNG, GIF');
	docform.submit.disabled = false;
	docform.submit.value = 'Upload Image';
	return false;
}
that seemed to do the trick
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

toLowerCase.match is the fault of the previous attempt.

Code: Select all

document.forms['docform'].elements['ep_image'].value.match(/\.(?:jpe?g|gif|png)$/i);
Post Reply