| Author |
Message |
pnclthnmstsh Hangin' Around

Joined: Oct 23, 2005 Posts: 46 Location: Portland, Or
|
Posted:
Mon Aug 28, 2006 5:56 pm |
|
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  |
|
|
|
 |
fkelly Moderator

Joined: Aug 30, 2005 Posts: 2062 Location: near Albany NY
|
Posted:
Mon Aug 28, 2006 7:06 pm |
|
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. |
|
|
|
 |
pnclthnmstsh Hangin' Around

Joined: Oct 23, 2005 Posts: 46 Location: Portland, Or
|
Posted:
Mon Aug 28, 2006 7:16 pm |
|
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 Moderator

Joined: Apr 30, 2004 Posts: 2757
|
Posted:
Tue Aug 29, 2006 12:43 pm |
|
Here's a way to do regular expressions in Javascript
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. |
|
|
|
 |
pnclthnmstsh Hangin' Around

Joined: Oct 23, 2005 Posts: 46 Location: Portland, Or
|
Posted:
Tue Aug 29, 2006 1:09 pm |
|
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  |
|
|
|
 |
|
|
|
|