0

I've been pulling my hair out trying to add a FILE input object to a form on one of my intranet web pages. I saw this article (HTML <input type='file'> File Selection Event) and wondered if this is why even though I have an [onSubmit] handler linked in the tag, as soon as I add the object, it no longer fires the onSubmit event at the form level. Is there a way to tie it all together so I can still validate the other input objects on the same form before going to the handler URL? I need this to work in IE8, and Chrome 20+ mostly. Any help would be greatly appreciated.

Javascript:

function CheckForm(theForm) {

    var formName = "form1";
    var formx = document.getElementById(formName);

    if (formx.appname.value == "") {
        alert("Please enter the \"APPLICATION NAME\"...");
        formx.appname.focus();
        return(false);
    }

    if (formx.vendor.value == "") {
        alert("Please enter the \"VENDOR NAME\"...");
        formx.vendor.focus();
        return(false);
    }

    if (formx.srcfile.value == "") {
        alert("Please select the "\FILE NAME\"...");
        return(false);
    }

    return (true);
}

HTML:

<form name="form1" id="form1" method="post" language="JavaScript" onSubmit="return CheckForm(this);return false;" action="appform2.asp">

<table width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#c0c0c0">
    <tr>
        <td>Product Name / Version</td>
        <td>
            <input type="text" id="appname" name="appname" size="50" maxlength="255"/>
        </td>
    </tr>
    <tr>
        <td>Vendor Name</td>
        <td>
            <input type="text" id="vendor" name="vendor" size="50" maxlength="255"/>
        </td>
    </tr>
    <tr>
        <td>Source Binaries</td>
        <td>
            <input type="file" name="srcfile" id="srcfile"/>
        </td>
    </tr>
</table>

<p>
    <input type="reset" name="b2" id="b2" value="Reset"/>
    <input type="submit" name="b1" id="b1" value="Submit"/>
</p>

</form>    
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Skatterbrainz
  • 1,077
  • 5
  • 23
  • 31
  • Can you get the code working in a JSFiddle?.. If you set it up so we can easily play with it, Im sure someone can help, Also, have you thought about using `jQuery.validate`?, no need to reinvent the wheel! – Anil Jul 23 '13 at 14:23
  • I'm a bit new to JS oddly enough. What's JSFiddle? Also, I agree about not reinventing the wheel. One issue I'm also seeing is that when I select a file using the FILE input object, it behaves very different in IE and CHrome. In IE it returns the full path and filename, while in Chrome it only returns the base filename. I need to get the whole path. – Skatterbrainz Jul 23 '13 at 14:59

0 Answers0