Forum Moderators: open

Message Too Old, No Replies

Form validation for array!

         

dreamcatcher

4:12 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I`m trying to do form validation for an array, but I`m getting stuck. I have a file uploader and I want to do a client side check to see that at least 1 file has been selected for upload.

So, my form fields are:


<input class="inputbox" type="file" name="user_files[]">
<input class="inputbox" type="file" name="user_files[]">
<input class="inputbox" type="file" name="user_files[]">

I really don`t know javascript, but this is what I came up with based on some code I found on a tutorial site:


function checkuploads()
{
for (i=0; i<document.userform[0].user_files.length; i++)
{
if (document.userform[0].user_files[i].value!= '')
{
user_input = document.userform[0].user_files[i].value;
}
}
if (user_input)
{
return false;
}
}

The error I get is something like document.userform[0].user_files.length is null or not an object.

Can anyone point me in the right direction?

Many thanks.

john_k

4:28 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take the brackets out of the name attributes

Next, do you have more than one FORM defined with the name "userform"? If not, then change the javascript to:


function checkuploads()
{
for (i=0; i<document.userform.user_files.length; i++)
{
if (document.userform.user_files[i].value!= '')
{
user_input = document.userform.user_files[i].value;
}
}
if (user_input)
{
return false;
}
}

Please post back here if that helps or not.

coopster

4:48 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>Take the brackets out of the name attributes

Unfortunately, the square brackets after the variable name tell PHP that's what makes it an array. ( How do I create arrays in a HTML <form>? [php.net] ) The link provided shows a number of different options for javascript processing as well, including some good stuff in the User Contributed notes. Something like this may work...

function checkuploads(form) { 
for (i=0; i<form.elements['user_files[]'].length; i++) {
if (form.elements['user_files[]'][i].value!= '') {
return true;
}
}
return false;
}
</script>
...
<form method="post" enctype="multipart/form-data" onsubmit="checkuploads(this);">

dreamcatcher

5:40 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks john_k, but like coopster mentions, the brackets are required as its a PHP file. Sorry I wasn`t a little clearer.

Never thought to check the PHP.net website, thanks coopster. Based on your code and what I read there, finally got it working:


function checkuploads(form)
{
var file;

for (i=0; i<form.elements['user_files[]'].length; i++)
{
if (form.elements['user_files[]'][i].value!= '')
{
file += form.elements['user_files[]'][i].value;
}
}
if (file)
{
return true;
}
else
{
alert('Please choose at least one file');
return false;
}
}

Thanks again! :)

john_k

5:59 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unfortunately, the square brackets after the variable name tell PHP that's what makes it an array.

Ah. I haven't done any coding in PHP, so that was news to me. I have always heard good things about PHP and find it odd that it imposes this non-standard restriction on the HTML.

DrDoc

6:15 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not a restriction... It's a feature :)
You automatically get an array!

Bernard Marx

8:22 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



..in PHP, but, for validation purists (only), it's no good.
Square bracket chars aren't valid for id or name attributes.

Here's some jibbering on the topic:
[jibbering.com ]

DrDoc

9:30 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...which is why I use :: to identify it as an array.
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Once I'm parsing the page, I simply do an array_push...

Bernard Marx

9:32 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Stop that! You're confusing me now.

coopster

10:25 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Me too ;)

>>Square bracket chars aren't valid for id or name attributes.

id attributes I agree, but you'll have to sell me on the name attribute. PHP requires you use the name [w3.org] attribute in this manner, not the id attribute.

The DTD specifies the syntax of HTML element content and attribute values using SGML tokens (e.g., PCDATA, CDATA, NAME, ID, etc.). The name attribute is defined in the DTD as type CDATA [w3.org], which is a sequence of characters from the document character set and may include character entities.

It is very confusing to me as well, but it seems there is a difference here.

Bernard Marx

7:50 am on Aug 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what the horse's mouth says:

[w3.org ]

..as DrDoc has already quoted.

Whether validation of this sort actually matters is another issue.

coopster

1:50 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



This is exactly where I was taking the discussion, thanks for the reply. I struggled with this when I first had to use PHP's array feature, the double brackets, []. It works, it's legal. (I still don't like it though; I'd rather the developers handle arrays much the same way Lincoln Stein did with Perl's cgi.pm module).

The link you provided, which is the quote mentioned by DrDoc earlier, refers to the ID and NAME tokens. Roll back in that link, one paragraph, to see the CDATA token. Now click the link I showed earlier, the name attribute. See it's token? CDATA, not ID and not NAME. Big difference here since CDATA is not restricting us to the character set mentioned in ID and NAME tokens.

Bernard Marx

2:38 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Confusing? Tell me about it!
I have great difficulty making my way around these references.

It says that ID & NAME are type, CDATA. Then it says:

For some HTML 4 attributes with CDATA attribute values, the specification imposes further constraints on the set of legal values for the attribute that may not be expressed by the DTD.

"not expressed by the DTD" - hmmm

Under the same bullet point, STYLE & SCRIPT are mentioned. The specification that imposes our possible restriction isn't under the same bullet, but the next.

ID and NAME tokens must begin with a letter ([A-Za-z]) and...

On the one hand, you'd expect (via usual "list logic") this specification to be part of a sub-list of the previous bullet, if it is linked to the first quote. On the other hand, however located, the statement plainly says "No, you can't do that" - just like many "well-respected" sources appear to think.

BUT, I'm starting to have doubts, and think you are right..

I'm getting confused, possibly, by name as an attribute, and name as a type of token...and the same with id too (Surely they could have highlighted this trap).

name = cdata [w3.org] [CI] (*source) [w3.org]
id = name [w3.org] (*source) [w3.org]

As you say, only ID is restricted, as it has token type name. Whilst NAME having token type CDATA doesn't have this restriction.

or...

coopster

11:12 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Exactly. Fun stuff, eh? Thanks for the discussion, very enjoyable -- coopster