Alert for description attachment | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| |
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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
It should be the ID: - description = document.getElementById("description");
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment
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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment
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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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;
-
}
-
}
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
The ID of the first text box should be "description1", not just "description", for the code to work.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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?
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
You just need to add an equals sign on line 71 as I've done in my previous post.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
Is the second description ID "description2"?
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
If you dynamically generate subsequent fields, it won't appear in the source.
What code are you using to generate more fields?
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,653
| | | re: Alert for description attachment
the web developer plugin (FF) has an option to show the complete (static and dynamic) source code of a page.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment
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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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);
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment
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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
Does it not work in any browser?
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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== "")
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment
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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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?
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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)
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment
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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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!
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment
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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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.
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
The ID in line 3 of the validation code should be "attachment"+i (without the 's').
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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?
| | Site Addict | | Join Date: Aug 2008 Location: United States
Posts: 769
| | | re: Alert for description attachment Quote:
Originally Posted by acoder 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: Alert for description attachment
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 ...
-
}
|  | | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|