Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> JavaScript
Author Message
pnclthnmstsh
Regular
Regular



Joined: Oct 23, 2005
Posts: 54
Location: Portland, Or

PostPosted: Mon Aug 28, 2006 5:56 pm Reply with quote

I'm trying to verify that a form field contains gif, jpg or png and what i've got does check, however it verifies that the field ONLY contains one of those. If anything else...like the rest of the URL to the image is in the field it returns the alert.
Code:
 

function imagecheck(){
var string1=document.validation.image.value
if (string1.indexOf("gif")==0 || string1.indexOf("jpg")==0 || string1.indexOf("png")==0){
}
else{
alert("Only gif, jpg and png images are allowed!")
document.validation.image.focus()
}
}


Thanks for any suggestions on how to allow other chars in that field too Smile

_________________
Only registered users can see links on this board! Get registered or login! 
View user's profile Send private message Visit poster's website Yahoo Messenger
fkelly
Former Moderator in Good Standing



Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY

PostPosted: Mon Aug 28, 2006 7:06 pm Reply with quote

You probably need to find the length of the field. Then do a substring on the last three characters. Then see if that substring equals gif or jpg or png. You might want to uppercase or lowercase the whole thing before doing any of those tests. Be careful because the indexes for these things start at 0. Your Javascript manual or the online documentation will tell you the exact syntax.
 
View user's profile Send private message Visit poster's website
pnclthnmstsh







PostPosted: Mon Aug 28, 2006 7:16 pm Reply with quote

Cool...thanks for the tip! Ya, I remember reading about all of that. This code was from a tut..well the part that says it's supposed to search for a string but yeah...doing it your way should eliminate alot of possible user errors...like GIF instead of gif.
 
evaders99
Former Moderator in Good Standing



Joined: Apr 30, 2004
Posts: 3221

PostPosted: Tue Aug 29, 2006 12:43 pm Reply with quote

Here's a way to do regular expressions in Javascript
http://www.evolt.org/article/Regular_Expressions_in_JavaScript/17/36435/

You should also do a server side check using whatever language your script is, especially if it is something uploaded to the server. Be careful and check file sizes, image sizes, content type to make sure it is indeed an image.

_________________
- Only registered users can see links on this board! Get registered or login! -

Need help? Only registered users can see links on this board! Get registered or login! 
View user's profile Send private message Visit poster's website
pnclthnmstsh







PostPosted: Tue Aug 29, 2006 1:09 pm Reply with quote

Thanks for the link and the advice evaders! This form (written in HTML/PHP) is just emails the info and has another script to remove any html tags so I think once I get this image check in it should be a safe form from most kinds if malware...I hope Wink
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> JavaScript

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©