Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Help with Form Regex for image File Uploads?

Quote Reply
Help with Form Regex for image File Uploads?
Hello

I need to find the right Regex to accept only user image files with the file names containing only ( 0-9, a-z, A-Z, underscores, hyphens ) of the file type .jpg , .JPG , .jpeg , .JPEG . All others must return an error...

I've tired several Regex found throughout the forums suggest by Alex, Paul, and a few others. They all seemed to fall short by accepting something than isn't valid for displaying images.

http://www.gossamer-threads.com/...orum.cgi?post=198814
http://www.gossamer-threads.com/...orum.cgi?post=158079
http://www.gossamer-threads.com/...orum.cgi?post=193435

I thought this should work but it doesn't...

^[\w^\s]+\.(jpg|JPG|jpeg|JPEG)$

From what I can see I need:

\w for (0-9, a-z, A-Z, and _ )

^/s for no whitespace

\. for the . before the file type

(jpg|JPG|jpeg|JPEG) for the file types

???? to return true for the hyphen character.



I just can't seem to get it all together to work properly!

Someone please help, I've spent to many hours trying to work this out! Crazy
Quote Reply
Re: [Jonze] Help with Form Regex for image File Uploads? In reply to
Something like this should work:

Code:
^[\w-]+\.(?:jpe?g|JPE?G)$

Last edited by:

Paul: Nov 8, 2002, 2:38 AM
Quote Reply
Re: [Paul] Help with Form Regex for image File Uploads? In reply to
Thank you Paul! Laugh

I stayed up until about 5:00 a.m last night trying to work this out. I did finally come up with:

^[a-zA-Z0-9_\-]+[^\s]*\.(jpg|JPG|jpeg|JPEG)$

It did work but yours somehow does the same things and it's much , much cleaner. I could have swore using just [\w] was verifying filenames with whitespaces and commas which didn't work.

Anywayz I've tested it thoroughly and Paul's regex returns only valid .jpg image filenames for upload columns....

^[\w-]+\.(?:jpe?g|JPE?G)$ Good stuff!



Well guess I better move on to figuring out why files submitted by the user can't be deleted upon modification by Admin..... + ColumnName can not contain the value '' Pirate Later


Quote Reply
Re: [Jonze] Help with Form Regex for image File Uploads? In reply to
I realise this is a very old thread but this problem is apparently still evident in Links 3. Using the regex above, users can only upload files with a valid filename or no file at all. If the file is deleted in Links admin, an error message appears that says :

Code:
Photo cannot contain the value ''

Can someone confirm this is a bug, as the regex appears to be working fine for uploads.
Quote Reply
Re: [aus_dave] Help with Form Regex for image File Uploads? In reply to
Hi,

Here's what I'm currently using without any problems... that I know of anyway. Smile

Code:
^|.*\.(?:jpe?g|JPE?G)$

There is a bug with certain field types I had to contact GT about recently. One that returns the error message that you posted when user fields are left blank. I don't know if that's your problem, but if that regex doesn't work talk to GT. They had a fix for me, but not released.

Hope that helps.