Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Junior Member
    Join Date
    Jul 2007
    Posts
    7
    Member #
    15595
    Hi,

    I've been toying around with this for hours and I can't figure out why it doesn't work, basically I need to validate two file input fields and make sure they are of a certain file type, I got it to work with one field but when I tried to add more Javascript to check a second field, it doesn't work (just copied and pasted what worked for the first field and referred it to the second field's value).

    Here is the javascript:
    HTML Code:
    <script type="text/javascript" language="JavaScript"><!--
    function ExtensionsOkay() {
    var extension = new Array();
    
    var fieldvalue1 = document.getElementById('fieldName1').value;
    var fieldvalue2 = document.getElementById('fieldName2').value;
    
    extension[0] = ".png";
    extension[1] = ".gif";
    extension[2] = ".jpg";
    extension[3] = ".jpeg";
    extension[4] = ".ico";
    extension[5] = ".cur";
    
    var thisext1 = fieldvalue1.substr(fieldvalue1.lastIndexOf('.'));
    var thisextt1 = thisext1.toLowerCase();
    for(var i = 0; i < extension.length; i++) {
        if(thisextt1 == extension[i]) { return true; }
        }
    $('#example3 .text1').text('Only images are allowed');
    return false;
    }
    
    // This second section makes the whole script fail, if I use just the first section, it works
    var thisext2 = fieldvalue2.substr(fieldvalue2.lastIndexOf('.'));
    var thisextt2 = thisext2.toLowerCase();
    for(var i = 0; i < extension.length; i++) {
        if(thisextt2 == extension[i]) { return true; }
        }
    $('#example3 .text2').text('Only images are allowed');
    return false;
    }
    //--></script>
    Here is the html of the form:
    HTML Code:
    <ul>
      <li id="example3" class="example">
        <p>This script even works inside a form</p>
        <form method="POST" id="MyForm" enctype="multipart/form-data" onsubmit="return ExtensionsOkay();">
          <input type="hidden" name="redirect" value="/thankyou.php">
          <input type="hidden" name="uploadedfilesaveinfo" value="uploadinfo.htm">
          Image to upload:
          <input type="file" name="FieldName" size="33" accept="image/*" maxlength="256" id="fieldName1">
          <input type="file" name="FieldName" size="33" accept="image/*" maxlength="256" id="fieldName2">
          <input type="submit" value="Upload my image">
        </form>
        <p class="text1"></p>
        <p class="text2"></p>
      </li>
    </ul>
    Please keep in mind that I am relatively new to javacript.

    Thanks a lot!

  2.  

  3. #2
    Junior Member
    Join Date
    Sep 2009
    Posts
    7
    Member #
    19966
    There are several reasons why the script is not working.

    1. In the code below
    $('#example3 .text1').text('Only images are allowed');
    return false;
    }

    You are unconditionally returning false and closing the function. (Use firefox and check the Tools->Error console )

    2. In the code below
    if(thisextt1 == extension[i]) { return true; }

    it is returning even just one of the file name is correct. it does not proceed to checking the second field

  4. #3
    Junior Member
    Join Date
    Jul 2007
    Posts
    7
    Member #
    15595
    Thanks! Ok, so I tried to fix some of it, how do I make it so returns false if just one if bad?

    Code:
    <script type="text/javascript" language="JavaScript"><!--
    function ExtensionsOkay() {
        var extension = new Array();
        
        // Step 1 of 2:
        // Replace MyForm with the name of your form and 
        //    replace FieldName with the upload field name.
        
        var fieldvalue1 = document.getElementById('fieldName1').value;
        var fieldvalue2 = document.getElementById('fieldName2').value;
        
        // Step 2 of 2:
        // Add the file name extensions that are okay (with 
        //    the period), for the variables with their numbers 
        //    in sequential order, as many or as few as needed, 
        //    starting with 0. (These are case sensitive.)
        
        extension[0] = ".png";
        extension[1] = ".gif";
        extension[2] = ".jpg";
        extension[3] = ".jpeg";
        extension[4] = ".ico";
        extension[5] = ".cur";
        
        
        // No other customization needed.
        var thisext1 = fieldvalue1.substr(fieldvalue1.lastIndexOf('.'));
        var thisextt1 = thisext1.toLowerCase();
        
        for(var i = 0; i < extension.length; i++) {
            if(thisextt1 == extension[i]) { 
                return true;
            }
            else
            {
                $('#example3 .text1').text('Only images are allowed');
                return false;
            }
        }
        
        // This second section makes the whole script fail, if I use just the first section, it works
        var thisext2 = fieldvalue2.substr(fieldvalue2.lastIndexOf('.'));
        var thisextt2 = thisext2.toLowerCase();
        
        for(var i = 0; i < extension.length; i++) {
            if(thisextt2 == extension[i]) { 
              return true; 
            }
            else
            {
                $('#example3 .text2').text('Only images are allowed');
                return false;
            }
        }
    }
    //--></script>


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search tags for this page

what is a extension validator service

Click on a term to search for related topics.
All times are GMT -6. The time now is 05:07 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com