Alert for description attachment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bonneylake
    Contributor
    • Aug 2008
    • 769

    Alert for description attachment

    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
    Code:
     <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
    Code:
    <!---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

    Code:
     if(description1.value== "")
     {
      alert ("Please fill in the description for your attachment");
    return false;
      }
    Thank you in advance,
    Rach
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Put your validation code in a function (let's call it validate()) and call it onsubmit:
    Code:
    <form ... onsubmit="return validate();">
    You'll also need to make sure that you access the form elements properly.

    Comment

    • bonneylake
      Contributor
      • Aug 2008
      • 769

      #3
      Originally posted by acoder
      Put your validation code in a function (let's call it validate()) and call it onsubmit:
      Code:
      <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

      Code:
       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)

      Code:
      <input type="text" name="description1" id="description" value="" />
      Thank you,
      Rach

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        It should be the ID:
        Code:
        description = document.getElementById("description");

        Comment

        • bonneylake
          Contributor
          • Aug 2008
          • 769

          #5
          Hey Acoder,

          That worked for one description, how would i make it check for multiple descriptions? here is what i have

          Code:
          var description = document.getElementById("description")
            if(description.value== "")
           {
            alert ("Please fill in the description for your attachment");
          return false;
            }
          Thank you
          Rach

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            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.
            Code:
            for (i = 0; i < upload_no; i++) {
               description = document.getElementById("description"+i);
            where upload_no is a variable containing the number of attachments.

            Comment

            • bonneylake
              Contributor
              • Aug 2008
              • 769

              #7
              Hey Acoder,

              here is what i have,but its not working right

              Code:
              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

              Code:
              <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

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                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.va lue == "") that you had earlier.

                Comment

                • bonneylake
                  Contributor
                  • Aug 2008
                  • 769

                  #9
                  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.va lue == "") 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,

                  Code:
                  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

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    I meant for the uploads variable, so you would have something like:
                    Code:
                    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;
                     }
                    }

                    Comment

                    • bonneylake
                      Contributor
                      • Aug 2008
                      • 769

                      #11
                      Originally posted by acoder
                      I meant for the uploads variable, so you would have something like:
                      Code:
                      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

                      Code:
                       <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

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        The ID of the first text box should be "descriptio n1", not just "descriptio n", for the code to work.

                        Comment

                        • bonneylake
                          Contributor
                          • Aug 2008
                          • 769

                          #13
                          Originally posted by acoder
                          The ID of the first text box should be "descriptio n1", not just "descriptio n", 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
                          Code:
                           <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
                          Code:
                          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

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            I notice two problems:
                            Code:
                            var uploads = parseInt(document.getElementById("uploads"));
                            should be
                            Code:
                            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.

                            Comment

                            • bonneylake
                              Contributor
                              • Aug 2008
                              • 769

                              #15
                              Originally posted by acoder
                              I notice two problems:
                              Code:
                              var uploads = parseInt(document.getElementById("uploads"));
                              should be
                              Code:
                              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
                              Code:
                              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

                              Comment

                              Working...