Hey Everyone,
well was hoping if someone could explain to me how to add an alert for the description fields of this multiple attachments script. The problem i think i am having is i don't know how to make it check multiple fields to make sure each field has been checked for a description before submitting. Here is what i have
on my form page - <input type="file" name="attachment1" id="attachments" value="1" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
-
Description <input type="text" name="description1" id="description" value="" />
-
<div id="moreUploads"></div>
-
<div id="moreUploadsLink" style="display:none;">
-
<input type="button" value="Attach another file"
-
onclick="javascript:addFileInput();" >
-
</div>
-
<input type="hidden" name="uploads" id="uploads" value="1">
where it goes after i click submit - <!---Inserts attachments into attachments.--->
-
<cfif structKeyExists(FORM, "uploads")>
-
<cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
-
<cfparam name="FORM.uploads" default="">
-
<cfloop list="#form.uploads#" index="counter">
-
<cfset currentDescription = form["description" & counter]>
-
<!--- verify the form field exists --->
-
<cfif structKeyExists(FORM, "attachment"& counter)>
-
<!--- try and upload it ...--->
-
<cftry>
-
<cfif Len(FORM["attachment#counter#"])>
-
<cffile action="upload" fileField="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
-
<cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
-
<!--- IF RUN INTO PROBLEMS WITH FILES NOT GOING INTO TABLE MAKE SURE EVERYTHING BETWEEN SOURCE="" IS ALL ON ONE LINE--->
-
<CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\ form\attachments\#filename#">
-
<cfquery name="attachment" datasource="CustomerSupport">
-
exec usp_CS_Insertattachments
-
'#Form.ID#','#evaluate(serialnum)#','#currentDescription#','#filename#','#Form.fk_addedBy#'
-
</cfquery>
-
</cfif>
-
<cfcatch><!--- do something here, e.g. display error message.. --->
-
</cfcatch>
-
</cftry>
-
</cfif>
-
</cfloop>
-
</cfif>
-
i tried this but clearly i am missing a lot - if(description1.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
Thank you in advance,
Rach
57 5109
Put your validation code in a function (let's call it validate()) and call it onsubmit: - <form ... onsubmit="return validate();">
You'll also need to make sure that you access the form elements properly.
Put your validation code in a function (let's call it validate()) and call it onsubmit: - <form ... onsubmit="return validate();">
You'll also need to make sure that you access the form elements properly.
Hey Acoder,
Ok i got the part in the form where it does the return validate(). But i am unsure about the access the form elements properly. this is what i been trying - if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
an i don't know if i should be getting the name of the id (for some reason can not remember which one i am suppose to get) - <input type="text" name="description1" id="description" value="" />
Thank you,
Rach
It should be the ID: - description = document.getElementById("description");
Hey Acoder,
That worked for one description, how would i make it check for multiple descriptions? here is what i have - var description = document.getElementById("description")
-
if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
Thank you
Rach
If you have been consistent in the naming of the description fields (which it seems you have been), then use a for loop and use the index to get the elements, e.g. - for (i = 0; i < upload_no; i++) {
-
description = document.getElementById("description"+i);
where upload_no is a variable containing the number of attachments.
Hey Acoder,
here is what i have,but its not working right -
-
var description = document.getElementById("description")
-
for (i = 0; i < uploads; i++) {
-
description = document.getElementById("description"+i);
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
i think i have the part where it says uploads screwed up.in my form i use this line to count - <input type="hidden" name="uploads" id="uploads" value="1">
an thought that replacing upload_no with uploads would be right since its suppose to keep the count but its not working right any ideas?
Thank you,
Rach
You again need to use the ID to access the uploads. You'll also need to parse it using parseInt to convert to an integer.
Secondly, you can remove the "var description " line (line 2) and add back the "if (description.value == "") that you had earlier.
You again need to use the ID to access the uploads. You'll also need to parse it using parseInt to convert to an integer.
Secondly, you can remove the "var description " line (line 2) and add back the "if (description.value == "") that you had earlier.
Hey Acoder,
Well i got the id for the hidden field, the id is also uploads. But am i suppose to be getting the id for the hidden field or for the description field? i redid it with the description field id here is what i have, - if(description.value== "")
-
{
-
for (i = 0; i < description; i++) {
-
description = document.getElementById("description"+i);
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}}
an i am not sure how to do parse because never done parse before.
Thank you,
Rach
I meant for the uploads variable, so you would have something like: - var uploads = parseInt(document.getElementById("uploads"));
-
for (i = 0; i < uploads; i++) {
-
description = document.getElementById("description"+i);
-
if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
}
I meant for the uploads variable, so you would have something like: - var uploads = parseInt(document.getElementById("uploads"));
-
for (i = 0; i < uploads; i++) {
-
description = document.getElementById("description"+i);
-
if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
}
Hey Acoder,
I copied what you have but its still letting me send with or without an attachment selected. here is the html again in case you need to see it, i might of just told you the wrong thing needed but not sure what i would use - <input type="file" name="attachment1" id="attachments" value="1" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
-
Description <input type="text" name="description1" id="description" value="" />
-
<div id="moreUploads"></div>
-
<div id="moreUploadsLink" style="display:none;">
-
<input type="button" value="Attach another file"
-
onclick="javascript:addFileInput();" >
-
</div>
-
<input type="hidden" name="uploads" id="uploads" value="1">
Thank you,
Rach
The ID of the first text box should be "description1", not just "description", for the code to work.
The ID of the first text box should be "description1", not just "description", for the code to work.
Hey Acoder,
I changed it to description 1 but i am still getting the same results.
here is what i have on the form -
<input type="file" name="attachment1" id="attachments" value="1" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
-
Description <input type="text" name="description1" id="description1" value="" />
-
<div id="moreUploads"></div>
-
<div id="moreUploadsLink" style="display:none;">
-
<input type="button" value="Attach another file" onclick="javascript:addFileInput();" >
-
</div>
-
<input type="hidden" name="uploads" id="uploads" value="1">
and here is what i have for the validation -
var uploads = parseInt(document.getElementById("uploads"));
-
for (i = 0; i < uploads; i++) {
-
description = document.getElementById("description"+i);
-
if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
}
Thank you,
Rach
I notice two problems: - var uploads = parseInt(document.getElementById("uploads"));
should be - var uploads = parseInt(document.getElementById("uploads").value);
the value, not the element is what needs to be parsed.
Secondly, you need to start the loop from 1, not 0.
I notice two problems: - var uploads = parseInt(document.getElementById("uploads"));
should be - var uploads = parseInt(document.getElementById("uploads").value);
the value, not the element is what needs to be parsed.
Secondly, you need to start the loop from 1, not 0.
Hey Acoder,
i am still getting errors
here is what i have - var uploads = parseInt(document.getElementById("uploads").value);
-
for (i = 1; i < uploads; i++) {
-
description = document.getElementById("description"+i);
-
if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
}
Thank you,
Rach
The loop should now loop till uploads, not one before, i.e. - for (i = 1; i <= uploads; i++) {
Have you got 'return true' at the end of the validate function?
The loop should now loop till uploads, not one before, i.e. - for (i = 1; i <= uploads; i++) {
Have you got 'return true' at the end of the validate function?
Hey Acoder,
Yes i do have a return true at the end. I don't know if this would help but this is what i have on the entire validate page. - function validate_form()
-
{
-
//VALIDATES TYPE OF HARDWARE FAILURE
-
var hardware = document.getElementById('hardwarefailure')
-
var tag = document.getElementsByTagName('select')
-
-
for(var i=0; i<=tag.length-1; i++){
-
//check each tag (tag[i]) to see if its hardware failure
-
//if it is, then see if its value is nothing
-
//Rachels Note (had to use name== instead of id because id is unique and the select each item does not need to be unique)
-
if(tag[i].name=="hardwarefailure" && tag[i].value =='')
-
{
-
alert ('Please Select Type of Hardware Failure');
-
return false;
-
}
-
}
-
//VALIDATES DEPT/VENDOR RESPONSIBILTYS
-
var dept = document.getElementById('deptvendor')
-
var tag2 = document.getElementsByTagName('select')
-
-
for(var i=0; i<=tag2.length-1; i++){
-
//check each tag (tag[i]) to see if its hardware failure
-
//if it is, then see if its value is nothing
-
if(tag2[i].name=="deptvendor" && tag2[i].value =='')
-
{
-
alert ('Please Select Dept/Vendor Responsibility');
-
return false;
-
}
-
}
-
//VALIDATES HAVE ALL PARTS BEEN RETURNED
-
var parts = document.getElementById('partsreturn')
-
var tag3 = document.getElementsByTagName('select')
-
-
for(var i=0; i<=tag3.length-1; i++){
-
//check each tag (tag[i]) to see if its hardware failure
-
//if it is, then see if its value is nothing
-
if(tag3[i].name=="partsreturn" && tag3[i].value =='')
-
{
-
alert ('Please Select Have all parts been returned');
-
return false;
-
}
-
}
-
-
//if(softhardware.value == ''){
-
//softhardware.value = 'NOW IT IS THIS' ;
-
//return false;
-
//}
-
-
var isChecked = document.userForm.assignees.checked;
-
var selected = document.getElementById('toBox')
-
var isSelected = false;
-
for(var i=0; i<=selected.options.length-1; i++){
-
if(selected.options[i].selected ){
-
isSelected = true;
-
break;
-
}
-
}
-
// now you have isChecked and isSelected set, combine the two
-
if (!isSelected && isChecked){
-
alert ("Please Select a assignee(s)");
-
return false;
-
}
-
if (!isChecked && isSelected){
-
alert ("Checkbox 'assignees' must be selected in order to send email to assignees");
-
return false;
-
}
-
-
-
-
var uploads = parseInt(document.getElementById("uploads").value);
-
for (i = 1; i < uploads; i++) {
-
description = document.getElementById("description"+i);
-
if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
}
-
-
-
-
return true;
-
}
an i am confused on what you mean by the loop, does it need to be changed?
Thank you,
Rach
You just need to add an equals sign on line 71 as I've done in my previous post.
You just need to add an equals sign on line 71 as I've done in my previous post.
Hey Acoder,
That worked for one description but when i tested out 2 fields and had the first field with a description and second one with no descriptoin it still submitted it. anyway to make it check every description field added? - var uploads = parseInt(document.getElementById("uploads").value);
-
for (i = 1; i <= uploads; i++) {
-
description = document.getElementById("description"+i);
-
if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
}
Thank you :),
Rach
Is the second description ID "description2"?
Is the second description ID "description2"?
Hey Acoder,
As far as i know it should be description2.But when i view the source i can not see that. But here is what i happen when i go an submit the attachment, i don't know if this would help but maybe it could explain what its doing. - <cfif structKeyExists(FORM, "uploads")>
-
<cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
-
<cfparam name="FORM.uploads" default="">
-
<cfloop list="#form.uploads#" index="counter">
-
<cfset currentDescription = form["description" & counter]>
-
<!--- verify the form field exists --->
-
<cfif structKeyExists(FORM, "attachment"& counter)>
-
<!--- try and upload it ...--->
-
<cftry>
-
<cfif Len(FORM["attachment#counter#"])>
-
<cffile action="upload" fileField="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
-
<cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
-
<!--- IF RUN INTO PROBLEMS WITH FILES NOT GOING INTO TABLE MAKE SURE EVERYTHING BETWEEN SOURCE="" IS ALL ON ONE LINE--->
-
<CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#"
-
destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#filename#">
-
<cfquery name="attachment" datasource="CustomerSupport">
-
exec usp_CS_Insertattachments
-
'#Form.ID#','#evaluate(serialnum)#','#currentDescription#','#filename#','#Form.fk_addedBy#'
-
</cfquery>
-
</cfif>
-
<cfcatch><!--- do something here, e.g. display error message.. --->
-
</cfcatch>
-
</cftry>
-
</cfif>
-
</cfloop>
-
</cfif>
Thank you,
Rach
If you dynamically generate subsequent fields, it won't appear in the source.
What code are you using to generate more fields?
the web developer plugin (FF) has an option to show the complete (static and dynamic) source code of a page.
You're absolutely right - I use it myself!
I should've pointed out that both the Web Developer and Firebug add-ons are extremely useful when debugging. With Firebug, you can inspect and modify any element on the page.
Hey Dormilich and Acoder,
Will definitely look up firebug. I have heard about it from reading a few posts,but i been staying with the oldest firefox on account of other web development tools i used. But i guess it don't matter now because it made me update anyway lol.
But anyway this is what i am using to do the multiple fields
this is the javascript - // JavaScript Document
-
<!--- Allows you to attach multiple files --->
-
<!---br is a break an left it so i will know later how to do a break--->
-
var upload_number = 2;
-
function addFileInput()
-
{
-
var d = document.createElement("div");
-
var l = document.createElement("input");
-
var file = document.createElement("input");
-
var text = document.createElement("input");
-
var nbsp = document.createTextNode("\u00a0");
-
var space = document.createTextNode("\u00a0");
-
<!--- var br = document.createElement("br");--->
-
var des = document.createTextNode("Description")
-
try {
-
file = document.createElement("<input type='file' name='attachment"+upload_number+"'>");
-
text = document.createElement("<input type='text' name='description"+upload_number+"'>");
-
l = document.createElement("<input type='button' onclick='javascript:removeFileInput("+upload_number+");'>");
-
} catch (e) {
-
file = document.createElement("input");
-
text = document.createElement("input");
-
}
-
d.setAttribute("id", "f"+upload_number);
-
file.setAttribute("type", "file");
-
file.setAttribute("name", "attachment"+upload_number);
-
text.setAttribute("type", "text");
-
text.setAttribute("name", "description"+upload_number);
-
l.setAttribute("id", "l"+upload_number);
-
l.setAttribute("type", "button");
-
l.setAttribute("onclick", "javascript:removeFileInput("+upload_number+")");
-
-
l.type="button";
-
l.value="Remove";
-
l.onclick="javascript:removeFileInput("+upload_number+")";
-
<!--- l.setAttribute("href", "javascript:removeFileInput("+upload_number+");");--->
-
<!--- l.appendChild(document.createTextNode("Remove"));--->
-
d.setAttribute("id", "f"+upload_number);
-
d.appendChild(file);
-
d.appendChild(nbsp);
-
d.appendChild(des);
-
<!--- d.appendChild(br);--->
-
d.appendChild(space);
-
d.appendChild(text);
-
d.appendChild(l);
-
document.getElementById("moreUploads").appendChild(d);
-
document.getElementById("uploads").value += "," + upload_number;
-
upload_number++;
-
}
-
-
function removeFileInput(i)
-
{
-
var elm = document.getElementById("f"+i);
-
document.getElementById("moreUploads").removeChild(elm);
-
document.getElementById("uploads").value = document.getElementById("uploads").value.replace(i,"");
-
-
}
this is the html - <input type="file" name="attachment1" id="attachments" value="1" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
-
Description <input type="text" name="description1" id="description1" value="" />
-
<div id="moreUploads"></div>
-
<div id="moreUploadsLink" style="display:none;">
-
<input type="button" value="Attach another file" onclick="javascript:addFileInput();" >
-
</div>
-
<input type="hidden" name="uploads" id="uploads" value="1">
an this is what happens once i upload - <!---Inserts attachments into attachments.--->
-
<cfif structKeyExists(FORM, "uploads")>
-
<cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "uploaded">
-
<cfparam name="FORM.uploads" default="">
-
<cfloop list="#form.uploads#" index="counter">
-
<cfset currentDescription = form["description" & counter]>
-
<!--- verify the form field exists --->
-
<cfif structKeyExists(FORM, "attachment"& counter)>
-
<!--- try and upload it ...--->
-
<cftry>
-
<cfif Len(FORM["attachment#counter#"])>
-
<cffile action="upload" fileField="form.attachment#counter#" destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\" nameconflict="MAKEUNIQUE">
-
<cfset filename = cffile.ClientFileName & "_" & form.id & "_" & counter & "." & cffile.ClientFileExt>
-
<!--- IF RUN INTO PROBLEMS WITH FILES NOT GOING INTO TABLE MAKE SURE EVERYTHING BETWEEN SOURCE="" IS ALL ON ONE LINE--->
-
<CFFILE ACTION="RENAME" SOURCE="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#CFFILE.ServerFile#"
-
destination="C:\Inetpub\Development\WWWRoot\RachelB\footprints\form\attachments\#filename#">
-
<cfquery name="attachment" datasource="CustomerSupport">
-
exec usp_CS_Insertattachments
-
'#Form.ID#','#evaluate(serialnum)#','#currentDescription#','#filename#','#Form.fk_addedBy#'
-
</cfquery>
-
</cfif>
-
<cfcatch><!--- do something here, e.g. display error message.. --->
-
</cfcatch>
-
</cftry>
-
</cfif>
-
</cfloop>
-
</cfif>
Thank you :),
Rach
The reason why it doesn't work is that you don't set the id, so add a line that sets the ID for the description elements.
The reason why it doesn't work is that you don't set the id, so add a line that sets the ID for the description elements.
Hey Acoder,
I am confused on where you want me to add this line an does this line need to be this - document.getElementById(description1)
Thank you,
Rach
On line 27 in the JavaScript code, you've set the description name, but not the ID: - text.setAttribute("name", "description"+upload_number);
so set the ID too: - text.setAttribute("id", "description"+upload_number);
Hey Acoder,
It still not acting right. On my html it has the name and id as description1 - <input type="text" name="description1" id="description1" value="" />
in the javascript do i need to change the name in the javascript from description to description1?
here is what i have on the javascript - // JavaScript Document
-
<!--- Allows you to attach multiple files --->
-
<!---br is a break an left it so i will know later how to do a break--->
-
var upload_number = 2;
-
function addFileInput()
-
{
-
var d = document.createElement("div");
-
var l = document.createElement("input");
-
var file = document.createElement("input");
-
var text = document.createElement("input");
-
var nbsp = document.createTextNode("\u00a0");
-
var space = document.createTextNode("\u00a0");
-
<!--- var br = document.createElement("br");--->
-
var des = document.createTextNode("Description")
-
try {
-
file = document.createElement("<input type='file' name='attachment"+upload_number+"'>");
-
text = document.createElement("<input type='text' name='description"+upload_number+"'>");
-
l = document.createElement("<input type='button' onclick='javascript:removeFileInput("+upload_number+");'>");
-
} catch (e) {
-
file = document.createElement("input");
-
text = document.createElement("input");
-
}
-
d.setAttribute("id", "f"+upload_number);
-
file.setAttribute("type", "file");
-
file.setAttribute("name", "attachment"+upload_number);
-
text.setAttribute("type", "text");
-
text.setAttribute("name", "description"+upload_number);
- text.setAttribute("id", "description"+upload_number);
-
l.setAttribute("id", "l"+upload_number);
-
l.setAttribute("type", "button");
-
l.setAttribute("onclick", "javascript:removeFileInput("+upload_number+")");
-
-
l.type="button";
-
l.value="Remove";
-
l.onclick="javascript:removeFileInput("+upload_number+")";
-
<!--- l.setAttribute("href", "javascript:removeFileInput("+upload_number+");");--->
-
<!--- l.appendChild(document.createTextNode("Remove"));--->
-
d.setAttribute("id", "f"+upload_number);
-
d.appendChild(file);
-
d.appendChild(nbsp);
-
d.appendChild(des);
-
<!--- d.appendChild(br);--->
-
d.appendChild(space);
-
d.appendChild(text);
-
d.appendChild(l);
-
document.getElementById("moreUploads").appendChild(d);
-
document.getElementById("uploads").value += "," + upload_number;
-
upload_number++;
-
}
-
-
function removeFileInput(i)
-
{
-
var elm = document.getElementById("f"+i);
-
document.getElementById("moreUploads").removeChild(elm);
-
document.getElementById("uploads").value = document.getElementById("uploads").value.replace(i,"");
-
-
}
Thank you,
Rach
Does it not work in any browser?
Does it not work in any browser?
Hey Acoder,
it doesn't work in either browser. It will block the first attachment and description that appears when the page loads, but any attachment and description you add afterwards it will let me submit with no problem.
Thank you,
Rach
Of course, this would be a good time to use a good debugging tool, but in the meantime, try adding an alert to check the value: - for (i = 1; i <= uploads; i++) {
-
description = document.getElementById("description"+i);
-
alert(";" + description.value + ";");
-
if(description.value== "")
Of course, this would be a good time to use a good debugging tool, but in the meantime, try adding an alert to check the value: - for (i = 1; i <= uploads; i++) {
-
description = document.getElementById("description"+i);
-
alert(";" + description.value + ";");
-
if(description.value== "")
Hey Acoder,
I did download the firebug. An not 100% sure on how to use it. But i have managed to open it up and click on dos and i can see the upload_number (which is show in my javascript) an with only 1 field and one description show it says 2 (although you can only physically see 1) if i click add it makes it 3.I don't know if that helps but i thought i would tell you.
An on the code you just gave me i picked a attachment and put the description test (for the first attachment and description present) i then clicked add and choose a 2nd file to upload an then clicked submit an i got the alert ;test;.
Thank you,
Rach
Thank you,
Rach
The upload_number is correct. It's one more than the number so that when you create another set of fields it uses that number and adds one at the end.
Well, with what you've showed, it's obviously going to submit because ;test; means that you input "test" for the description, so it will pass validation. Try with no input.
Hey Acoder,
i am a bit confused on what you mean for no input? if i don't put a description for the first description present it will not send, however, if don't put a description for the 2nd it will still send.
Thank you,
Rach
Oh right, I see. So you tested with "test" for the first description and "" (blank) for the second description? Is that correct?
Does it show two alerts when you have two descriptions?
Oh right, I see. So you tested with "test" for the first description and "" (blank) for the second description? Is that correct?
Does it show two alerts when you have two descriptions?
Hey Acoder,
Yes that is how i tested it. first description with the word test, 2nd description with nothing.
when i click submit a pop up box appears says ;test;and then submits.
Thank you,
Rach
So that means it doesn't show the second alert. Is there an error?
Try the following: - Make the same test with two descriptions, one filled and one blank.
- Now right-click the description text box and click on "Inspect Element".
- Firebug will pop up with the element highlighted in code.
- Look at that code and see what it shows (you can right-click to copy)
Hey Acoder,
well i have no clue if i did this right or not. an was not sure if you wanted the html or the dom part. but here is the html part. - <th class="sectiontitle" colspan="7">Attachments</th>
-
</tr>
-
<tr>
-
<td class="indent"/>
-
<td>
-
<input id="attachments" type="file" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" value="1" name="attachment1"/>
-
Description
-
<input id="description1" type="text" value="" name="description1"/>
-
<div id="moreUploads">
-
<div id="f2">
-
<input type="file" name="attachment2"/>
-
Description
-
<input id="description2" type="text" name="description2"/>
-
<input id="l2" type="button" onclick="javascript:removeFileInput(2)" value="Remove"/>
-
</div>
-
</div>
-
<div id="moreUploadsLink" style="display: block;">
-
</div>
-
<input id="uploads" type="hidden" value="1,2" name="uploads"/>
-
</td>
-
</tr>
-
</tbody>
-
</table>
-
in the dom it only showed me
prefix
null
namespaceURI
null
nodeValue
null
Thank you,
Rach
It was the HTML part and I know now where the problem lies.
Have a look at the uploads hidden field. Now I assumed that that just contained the number of uploads, but rather it contains the numbers of the attachment fields being uploaded. However, there is a value that you're using that can be used instead, i.e. upload_number. Remember this is always one more than the current number of uploads, so you can simply replace: - var uploads = parseInt(document.getElementById("uploads").value);
-
for (i = 1; i <= uploads; i++) {
with - for (i = 1; i < upload_number; i++) {
See how useful a good debugging tool is!
Hey Acoder,
althought i still don't fully understand the firebug (yet), i am liking it an can see many other useful things i can use it for :).But i got one last challenge out of this. Is there anyway to make it so that if a user don't have an attachment picked out that it wont ask for the description? because if someone doesn't upload anything it still asks me to fill out the description
here is what i have on my validatoin page - for (i = 1; i <= upload_number; i++) {
-
description = document.getElementById("description"+i);
-
-
if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
}
Thank you :),
Rach
Get the attachment element first, i.e. with ID "attachment"+i, check if it's not empty. If it isn't, then make the check on the description, otherwise there's no need for validation.
Get the attachment element first, i.e. with ID "attachment"+i, check if it's not empty. If it isn't, then make the check on the description, otherwise there's no need for validation.
Hey Acoder,
I clearly did something wrong because now it won't at all.an i made sure i got the right id. - for (i = 1; i <= upload_number; i++) {
-
description = document.getElementById("description"+i);
-
attachment = document.getElementById("attachments"+i);
-
if attachment.value== "")
-
{
-
alert("Please choose a attachment");
-
return false;
-
}
-
else if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
}
Thank you,
Rach
You need the changes that you added for description in post #29 above. You need to add the ID for the file variable and also make sure the ID is "attachment1", "attachment2", etc.
Finally, on line 4 in the previous post, you forgot the opening ( in the if statement.
You need the changes that you added for description in post #29 above. You need to add the ID for the file variable and also make sure the ID is "attachment1", "attachment2", etc.
Finally, on line 4 in the previous post, you forgot the opening ( in the if statement.
Hey Acoder,
here is what i got but still not working right
the javascript - // JavaScript Document
-
<!--- Allows you to attach multiple files --->
-
<!---br is a break an left it so i will know later how to do a break--->
-
var upload_number = 2;
-
function addFileInput()
-
{
-
var d = document.createElement("div");
-
var l = document.createElement("input");
-
var file = document.createElement("input");
-
var text = document.createElement("input");
-
var nbsp = document.createTextNode("\u00a0");
-
var space = document.createTextNode("\u00a0");
-
<!--- var br = document.createElement("br");--->
-
var des = document.createTextNode("Description")
-
try {
-
file = document.createElement("<input type='file' name='attachment"+upload_number+"'>");
-
text = document.createElement("<input type='text' name='description"+upload_number+"'>");
-
l = document.createElement("<input type='button' onclick='javascript:removeFileInput("+upload_number+");'>");
-
} catch (e) {
-
file = document.createElement("input");
-
text = document.createElement("input");
-
}
-
d.setAttribute("id", "f"+upload_number);
-
file.setAttribute("type", "file");
-
file.setAttribute("name", "attachment"+upload_number);
-
file.setAttribute("id", "attachment"+upload_number);
-
text.setAttribute("type", "text");
-
text.setAttribute("name", "description"+upload_number);
-
text.setAttribute("id", "description"+upload_number);
-
l.setAttribute("id", "l"+upload_number);
-
l.setAttribute("type", "button");
-
l.setAttribute("onclick", "javascript:removeFileInput("+upload_number+")");
-
-
l.type="button";
-
l.value="Remove";
-
l.onclick="javascript:removeFileInput("+upload_number+")";
-
<!--- l.setAttribute("href", "javascript:removeFileInput("+upload_number+");");--->
-
<!--- l.appendChild(document.createTextNode("Remove"));--->
-
d.setAttribute("id", "f"+upload_number);
-
d.appendChild(file);
-
d.appendChild(nbsp);
-
d.appendChild(des);
-
<!--- d.appendChild(br);--->
-
d.appendChild(space);
-
d.appendChild(text);
-
d.appendChild(l);
-
document.getElementById("moreUploads").appendChild(d);
-
document.getElementById("uploads").value += "," + upload_number;
-
upload_number++;
-
}
-
-
function removeFileInput(i)
-
{
-
var elm = document.getElementById("f"+i);
-
document.getElementById("moreUploads").removeChild(elm);
-
document.getElementById("uploads").value = document.getElementById("uploads").value.replace(i,"");
-
-
}
-
-
the validation -
for (i = 1; i <= upload_number; i++) {
-
description = document.getElementById("description"+i);
-
attachment = document.getElementById("attachments"+i);
-
if (attachment.value== ""){
-
alert("Please choose a attachment");
-
return false;
-
}
-
else if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
}
Thank you,
Rach
The ID in line 3 of the validation code should be "attachment"+i (without the 's').
The ID in line 3 of the validation code should be "attachment"+i (without the 's').
Hey Acoder,
Now its working correctly. But one last thing that i got to add to it. I don't know if its possible but is there anyway to make it so it don't alert if no attachment nor description is filled out? - for (i = 1; i <= upload_number; i++) {
-
description = document.getElementById("description"+i);
-
attachment = document.getElementById("attachment"+i);
-
if (attachment.value== ""){
-
alert("Please choose a attachment");
-
return false;
-
}
-
else if(description.value== "")
-
{
-
alert ("Please fill in the description for your attachment");
-
return false;
-
}
-
}
Thank you,
Rach
Would this be in the case where there's only one attachment or could there be more than one attachment all of which could be empty?
Would this be in the case where there's only one attachment or could there be more than one attachment all of which could be empty?
Hey Acoder,
Well i am saying basically if a user did not choose an attachment nor a description that it would still let me submit instead of it asking for me to choose an attachment.because right now if someone does not choose an attachment nor writes a description they still have to put something therein order to click submit.
Thank you,
Rach
I think you only need to check attachment1 then because the description is checked afterwards anyway. You can put an if statement around the whole validation code which checks that attachment1 is not empty: - if (document.getElementById("attachment1").value != "") {
-
for ...
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Wald |
last post by:
Hello group,
I've got a script here that sends emails with an attachment to
an email address that is retrieved from an html form. The
email sending code is include below.
The problem: when...
|
by: knoak |
last post by:
Hi there,
I've found a script at these great Google fora. a script
to send emails with attachments.
The script is below this message, name etc. aren't mine, but from
the original post.
My...
|
by: Tasha's Dad |
last post by:
A description of the problem:
1) Go to a page with various settings and a timeout (forces re-login
if over 10 minutes)
2) Before the timeout, make some changes to settings.
3) Press a "reset to...
|
by: Aquila Deus |
last post by:
Hi all!
I found an interesting way to implement strong-typed object attactment
with generic:
// Attachment should be a Dictionary<K,V> or some user-defined
container, rather
// than...
|
by: Marc Castrechini |
last post by:
With SmartNavigation=true a call to display a simple javascript alert
returns in IE showing an "Invalid Pointer" error.
Apparently when SmartNavigation is disabled then the alert works correctly....
|
by: TomislaW |
last post by:
I am sending word document with e-mail from asp.net 2.0 application.
I read doc file like this:FileStream fs =
System.IO.File.Open(docPath,FileMode.Open,FileAccess.Read,FileShare.Read);
Then...
|
by: Wayne Deleersnyder |
last post by:
Hi All,
I'm trying to create a function that will cause a pop-up alert to
appear if dates which were chosen from a drop-down list were invalid
on a page. There's 4 dates, so there's the...
|
by: deepaks85 |
last post by:
Dear Sir,
I have created a simple request form which will be mailed to me.
Now I want to attach files and send it through that request form.
For this I am using the following script:
...
|
by: pantagruel |
last post by:
Hi,
the following works in FF but not IE:
Object.prototype.describe=function(){alert("Description: " +
this);return this}
alert.describe();
The question I have is mainly how can one go about...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |