SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Berlin
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Validation before Upload

    Hi,

    I want to use a JavaScript form validation for (multiple) file uploads. (Server-Side validation when havin multiple uploads unfortunately will give a error message after, some files might be already uploaded...)

    I tried following, which works fine for type="text", but not type="file", what do I have to change???

    PHP Code:
    /***********************************************
    * Required field(s) validation v1.10- By NavSurf
    * Visit Nav Surf at http://navsurf.com
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/

    function formCheck(formobj){
        
    // Enter name of mandatory fields
        
    var fieldRequired = Array("title""description""auth_name""thumnail");
        
    // Enter field description to appear in the dialog box
        
    var fieldDescription = Array("Titel""Beschreibung: (maximal 255 Zeichen)""Name des Autors""Datei auswaelen");
        
    // dialog message
        
    var alertMsg "Bitte füllen Sie folgende Felder aus: \n";
        
        var 
    l_Msg alertMsg.length;
        
        for (var 
    0fieldRequired.lengthi++){
            var 
    obj formobj.elements[fieldRequired[i]];
            if (
    obj){
                switch(
    obj.type){
                case 
    "select-one":
                    if (
    obj.selectedIndex == -|| obj.options[obj.selectedIndex].text == ""){
                        
    alertMsg += " - " fieldDescription[i] + "\n";
                    }
                    break;
                case 
    "select-multiple":
                    if (
    obj.selectedIndex == -1){
                        
    alertMsg += " - " fieldDescription[i] + "\n";
                    }
                    break;
                case 
    "text":
                case 
    "textarea":
                    if (
    obj.value == "" || obj.value == null){
                        
    alertMsg += " - " fieldDescription[i] + "\n";
                    }
                    break;
                default:
                }
                if (
    obj.type == undefined){
                    var 
    blnchecked false;
                    for (var 
    0obj.lengthj++){
                        if (
    obj[j].checked){
                            
    blnchecked true;
                        }
                    }
                    if (!
    blnchecked){
                        
    alertMsg += " - " fieldDescription[i] + "\n";
                    }
                }
            }
        }

        if (
    alertMsg.length == l_Msg){
            return 
    true;
        }else{
            
    alert(alertMsg);
            return 
    false;
        }
    }


    <
    form enctype="multipart/form-data" method="post"  name="formcheck" onsubmit="return formCheck(this);">
    <
    input type="hidden" name="MAX_FILE_SIZE" value="15728640"><div class="form">

    <
    input name="thumnail" type="file">
    <
    input name="userfile" type="file">
    <
    input type="hidden" name="language" value="de">
    <
    input type="text" name="title" size="45" maxlength="100" />
    <
    textarea name="description" cols="40" rows="8"></textarea>
    <
    input type="text" name="auth_name" value="Username" size="45" maxlength="60" />
    <
    input type="checkbox" name="show_submitter" value="1" /> 
    <
    input type="hidden" name="d_op" value="Add" />
    <
    input type="submit" value="Datei hinzuf&uuml;gen" />
    </
    form
    Thanks for Help

    Fl÷zen

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I cant get on with names so use ids

    PHP Code:
    in the script
        
    var fieldRequired = Array("tom","auth_name","title");

                case 
    "text":
                    if (
    obj.value == "" || obj.value == null){
                        
    alertMsg += " - " fieldDescription[i] + "\n";
                    }
                    break;
                case 
    "hidden":
                    if (
    obj.value == "" || obj.value == null){
                        
    alertMsg += " - " fieldDescription[i] + "\n";
                    }
                    break;

    the HTML


    <input name="thumnail" id="thumnail" type="file" onchange="document.getElementById('tom').value=this.value;">
    <
    input name="userfile" type="file">
    <
    input type="hidden" name="language" value="de">
    <
    input type="text" name="title" id="title" size="45" maxlength="100" />
    <
    textarea name="description" cols="40" rows="8"></textarea>
    <
    input type="text" id="auth_name" value="Username" size="45" maxlength="60" />
    <
    input type="checkbox" name="show_submitter" value="1" />
    <
    input id="tom" type="hidden">
    <
    input type="hidden" name="d_op" value="Add" />
    <
    input type="submit" value="Datei hinzuf&uuml;gen" />
    <
    input type="button" name="" value="test"onclick="return formCheck(this);">
    </
    form>
    </
    body

  3. #3
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Berlin
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK thanks alot - now this works fine!

    I love Sitepoint


Bookmarks

Posting Permissions

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