Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 26th, 2005, 03:35 AM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
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
Reply With Quote
  #2 (permalink)  
Old July 26th, 2005, 04:29 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
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

Reply With Quote
  #3 (permalink)  
Old July 26th, 2005, 07:32 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
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
Reply With Quote
  #4 (permalink)  
Old July 27th, 2005, 08:57 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
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
Reply With Quote
  #5 (permalink)  
Old July 28th, 2005, 03:15 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
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

Reply With Quote
  #6 (permalink)  
Old July 28th, 2005, 06:43 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:15 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.