Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,389 Programmers. There are 1,365 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Help with checking for special characters in Array

 
Reply to this topicStart new topic

Help with checking for special characters in Array, Check for special characters before file upload

Mitchell2038
post 9 May, 2008 - 09:36 AM
Post #1


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 9



I have an area where I upload several images and the code worked well until someone started uploading images with single quotes. Example: Mike's Pictures.jpg

So I tried to correct it in PHP however thats an issue in itself so I am relying on JAVA to verify there are no special characters in the file name prior to uploading the images instead.

All of my document.PostForm checks work except this one on the array area shown below. The Java does work in other parts of my sites code. Just not with the image array.

It appears the Java script never matches to the array and I cant seem to get the check for special characters to happen.

There is No error output. I am a PHP programmer and Im sure its a simple fix I just need a second set of eyes to tell me where the issue is.

CODE


I have tried doing stuff like images[0], images[1] etc with no luck using document.PostForm.images[0].length

My JAVA Check for Special Characters.

var iChars = "!@#$%^&*()+=-[]';,/{}|<>?";

for (var i = 0; i < document.PostForm.images[].length; i++) {
   if (iChars.indexOf([b]document.PostForm.images[][/b].charAt(i)) != -1)
   {
   document.write("Image file name containts special characters. \n These are not allowed.\n Please rename the files and try again.");
   return false;
   }}


My ARRAY IN HTML AREA OF SITE
<tr>
<td align=right valign=top>Images:</td>
<td>
  <input type=file name="images[]"><br>
  <input type=file name="images[]"><br>
  <input type=file name="images[]"><br>
  <input type=file name="images[]"><br>
</td>
</tr>



This post has been edited by Mitchell2038: 9 May, 2008 - 09:51 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


pbl
post 9 May, 2008 - 09:50 AM
Post #2


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 849

Your code is Java Script
You are in the Java forum
Beside having 4 characters in common in their name there are no relation at all between the 2
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Mitchell2038
post 9 May, 2008 - 09:52 AM
Post #3


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 9

I have an area where I upload several images and the code worked well until someone started uploading images with single quotes. Example: Mike's Pictures.jpg

So I tried to correct it in PHP however thats an issue in itself so I am relying on JAVA to verify there are no special characters in the file name prior to uploading the images instead.

All of my document.PostForm checks work except this one on the array area shown below. The Java does work in other parts of my sites code. Just not with the image array.

It appears the Java script never matches to the array and I cant seem to get the check for special characters to happen.

There is No error output. I am a PHP programmer and Im sure its a simple fix I just need a second set of eyes to tell me where the issue is.

CODE


I have tried doing stuff like images[0], images[1] etc with no luck using document.PostForm.images[0].length

My JAVA Check for Special Characters.

var iChars = "!@#$%^&*()+=-[]';,/{}|<>?";

for (var i = 0; i < document.PostForm.images[].length; i++) {
   if (iChars.indexOf([b]document.PostForm.images[][/b].charAt(i)) != -1)
   {
   document.write("Image file name containts special characters. \n These are not allowed.\n Please rename the files and try again.");
   return false;
   }}


My ARRAY IN HTML AREA OF SITE
<tr>
<td align=right valign=top>Images:</td>
<td>
  <input type=file name="images[]"><br>
  <input type=file name="images[]"><br>
  <input type=file name="images[]"><br>
  <input type=file name="images[]"><br>
</td>
</tr>

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

1lacca
post 9 May, 2008 - 09:55 AM
Post #4


code.rascal

Group Icon
Joined: 11 Aug, 2005
Posts: 3,192

Moved to javascript forum.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Mitchell2038
post 9 May, 2008 - 09:56 AM
Post #5


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 9

QUOTE(1lacca @ 9 May, 2008 - 09:55 AM) *

Moved to javascript forum.



WOW your fast. I didnt even have time to apologize for not knowing the difference between Java and Java script.

Thanks I can tell Im going to like these forums.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

1lacca
post 9 May, 2008 - 10:01 AM
Post #6


code.rascal

Group Icon
Joined: 11 Aug, 2005
Posts: 3,192

QUOTE
for (var i = 0; i < document.PostForm.images[].length; i++) {
if (iChars.indexOf(document.PostForm.images[].charAt(i)) != -1)

I think these lines need some attention, because you don't index into the images array. I think you'll need two for loops, one that iterates the images array, the other to iterate the special characters string (character array).


No problem, enjoy your stay smile.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Mitchell2038
post 9 May, 2008 - 10:07 AM
Post #7


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 9

QUOTE(1lacca @ 9 May, 2008 - 10:01 AM) *

QUOTE
for (var i = 0; i < document.PostForm.images[].length; i++) {
if (iChars.indexOf(document.PostForm.images[].charAt(i)) != -1)

I think these lines need some attention, because you don't index into the images array. I think you'll need two for loops, one that iterates the images array, the other to iterate the special characters string (character array).


No problem, enjoy your stay smile.gif


I figure I have to loop through at some point but I have tried doing stuff like this below but with no luck. It never sees my array.

CODE

if (!document.PostForm.images[0].length) {
     document.write("NO IMAGE");
     }
if (!document.PostForm.images[1].length) {
     document.write("NO IMAGE");
     }



<tr>
<td align=right valign=top>Images:</td>
<td>
  <input type=file name="images[0]"><br>
  <input type=file name="images[1]"><br>
  <input type=file name="images[2]"><br>
  <input type=file name="images[3]"><br>
</td>
</tr>


User is offlineProfile CardPM
Go to the top of the page
+Quote Post

1lacca
post 9 May, 2008 - 10:15 AM
Post #8


code.rascal

Group Icon
Joined: 11 Aug, 2005
Posts: 3,192

See this for help on accessing forms from Javascript.
However I think Javascript should pass the special characters to the server OK, and you could filter special characters in PHP easier on the server side.


User is offlineProfile CardPM
Go to the top of the page
+Quote Post

PsychoCoder
post 9 May, 2008 - 10:28 AM
Post #9


ToCode || !ToCode

Group Icon
Joined: 26 Jul, 2007
Posts: 5,858

You have another thread started for this very question. Please do not start multiple threads for the same question. Topics merged
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Mitchell2038
post 9 May, 2008 - 10:49 AM
Post #10


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 9

QUOTE(PsychoCoder @ 9 May, 2008 - 10:28 AM) *

You have another thread started for this very question. Please do not start multiple threads for the same question. Topics merged


I accidentally posted the original message to Java and created a second one in Java script at the same time the moderator moved the one to the other.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Mitchell2038
post 9 May, 2008 - 05:32 PM
Post #11


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 9

I FOUND A SOLUTION! WOOHOO

The quick and dirty is to give it an ID

I still need to write a look to go through each but you get the idea.

CODE

                    if(document.getElementById("f1").value != "")
                            {
                var iChars = "!@#$%^&*()+=[]';,/{}|<>?";
                            for (var i = 0; i < document.getElementById("f1").value.length; i++) {
                              if (iChars.indexOf(document.getElementById("f1").value.charAt(i)) != -1) {
                              alert ("Your file name contains special characters.");
                              return false;
                              }}}



    <td>
        <input id="f1" type=file name="images[]"><br>
        <input id="f2" type=file name="images[]"><br>
        <input id="f3" type=file name="images[]"><br>
        <input id="f4" type=file name="images[]"><br>
        <input id="f5" type=file name="images[]"><br>
        <input id="f6" type=file name="images[]"><br>
        <input id="f7" type=file name="images[]"><br>
        <input id="f8" type=file name="images[]"><br>
    </td>
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 04:54AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month