i want to use javascript for my conditional statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simon2x1
    New Member
    • Dec 2008
    • 123

    i want to use javascript for my conditional statement

    i have an upload form that upload image into a folder i want the upload field not to post a blank field and also not to allow any image which is not gif, jpeg and png not to be uploaded how can i do these with javascript. the code below is my upload form code

    Code:
     <form name="the_form" id="the_form" method="POST" action="uploaded.php" enctype="multipart/form-data">
           <input name="images[]" type="file"  id="field_1" > 
         <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
     <input name="submit" type="submit" value="submit" />
     </form>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    I assume you know how to add form validation. Add to that a check on the form file field, if your browser gives access to the field. You can access the value as follows:
    Code:
    document.getElementById("the_form").elements["images[]"].value
    With the image types, you can only check the extension.

    Comment

    • simon2x1
      New Member
      • Dec 2008
      • 123

      #3
      i am a beginner in java script, i dnt know how to validate a form. pls i would appreciate it if u would help with my "upload field so that not to post a blank field and also not to allow any image which is not gif, jpeg and png not to be uploaded "

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The basic components for form validation:
        1. an onsubmit handler:
        Code:
        <form ... onsubmit="return validateForm()">
        2. A validation function:
        Code:
        function validateForm(form) {
        ...
        }
        3. return false for failures:
        Code:
        if (elem.value.length == 0) {
            alert("Error!");
            elem.focus();
            return false;
        }
        4. If validation passes completely, return true:
        Code:
        // validate all fields
            ...
            // no more validation left:
            return true
        }//end of function

        Comment

        • simon2x1
          New Member
          • Dec 2008
          • 123

          #5
          i have added the validation to my code below it sill did not work pls i appreciate it if you help me fix this problem don't forget i want the upload field not to post a blank field and also not to allow any image which is not gif, jpeg and png not to be uploaded

          Code:
          <html>
          <head>
          <title></title>
          <head>
          <script language="JavaScript">
          function validateForm(form) {
          
            if (elem.value.length == 0) {
                  alert("Error!");
                  elem.focus();
                  return false;
              }else{
          document.getElementById("the_form").elements["images[]"].value
          	}
          }
          </script>
          
          <body>
          <form  method="POST" action="uploaded.php" enctype="multipart/form-data" onsubmit="return validateForm()">
          <input name="images[]" type="file"  id="field_1" > 
          <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
          <input name="submit" type="submit" value="submit" />
           </form>
          <body>
          </html>

          Comment

          • simon2x1
            New Member
            • Dec 2008
            • 123

            #6
            still need help with the validation

            i have added the validation to my code below it sill did not work pls i appreciate it if you help me fix this problem don't forget i want the upload field not to post a blank field and also not to allow any image which is not gif, jpeg and png not to be uploaded

            Code:
            <html>
            <head>
            <title></title>
            <head>
            <script language="JavaScript">
            function validateForm(form) {
            
              if (elem.value.length == 0) {
                    alert("Error!");
                    elem.focus();
                    return false;
                }else{
            document.getElementById("the_form").elements["images[]"].value
            	}
            }
            </script>
            
            <body>
            <form  method="POST" action="uploaded.php" enctype="multipart/form-data" onsubmit="return validateForm()">
            <input name="images[]" type="file"  id="field_1" > 
            <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
            <input name="submit" type="submit" value="submit" />
             </form>
            <body>
            </html>

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              "elem" on line 8 has to refer to an element, otherwise you will get an error. You can use the id "field_1":
              Code:
              var elem = document.getElementById("field_1");

              Comment

              • Bassem
                Contributor
                • Dec 2008
                • 344

                #8
                Line 13, You got the value of an element but you don't use it.
                At the end of the validation function you didn't return true.

                Comment

                Working...