Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 26th, 2005, 03:35 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default Detect image suffix?

Hello Javascripters. Is there some client side method out there to detect an image suffix not using a string comparison?

My objective is to not allow any images other than .jpg and .gif files to be uploaded to an image library. I realize I can do this by massaging/comparing the string I get from the form element. Due to the case sensitivity of JS I would like to do it a bit smarter. Any ideas? This would also eliminate the possibility of awkward users uploading doc and other strange things that do not belong in an image library.


TYIA

Wind is your friend
Matt
__________________
Wind is your friend
Matt
 
Old July 26th, 2005, 04:29 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Matt,

Don't know of a way without string comparison, but you could use a case insensitive regular expression, something like...
Code:
function isImg(fileName){
    return /\.(gif|jpe?g)$/i.test(fileName);
}
HTH,

Chris

 
Old July 26th, 2005, 07:32 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

After reading my question I realized I asked it poorly. Nice work - Your answer is exactly what I was looking for. Thanking you very much and you have a fine day.

Wind is your friend
Matt
 
Old July 27th, 2005, 08:57 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Chris My function is working however is not working with a mixture of case, what have I missed? My REG EX experience is poor, I should work on it:

    function validate()
    {
       if (document.updateForm.File1.value=="")
       {
          alert('Click browse to select a file for upload to proceed');
          updateForm.File1.focus();
          return (false);
       }
       if ((/.(gif|jpe?g)$/i.test(document.updateForm.File1.value))==false)
       {
           alert('You may only upload .jpg, .jpeg, or .gif images (in case sensitive)');
           return (false);
       }
           return(true);
    }

TYIA

Wind is your friend
Matt
 
Old July 28th, 2005, 03:15 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Your function seems to work fine for me casewise - the i at the end of the expression makes the whole thing case insensitive. Can you give an example of what is failing e.g. a filename and whether it should pass / fail?

BTW the period at the start of the expression needs to be escaped - '\.' instead of '.', otherwise it will allow any character before the file extension.

Thanks,

Chris

 
Old July 28th, 2005, 06:43 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

I just tryed it on my laptop, things work fine there. The production machine is on Defence Restricted Network, I am experiencing some wierd caching issues that seem to be out of my control - I believe this is what made the function behave badly. Thanks again Chris

Wind is your friend
Matt





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can we detect a broken image mat41 Classic ASP Basics 5 February 21st, 2008 07:14 PM
Detect Username dparsons ASP.NET 1.0 and 1.1 Professional 2 April 12th, 2006 02:20 PM
Help detect error elania Beginning PHP 4 February 14th, 2005 09:55 PM
Detect srcElement on Embedded Image TSEROOGY Javascript 8 August 29th, 2003 01:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.