Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation for file extension for an UPLOAD file....

Status
Not open for further replies.

aspnewbee

MIS
Jul 11, 2004
34
US
Hi I am trying to validate the files that are being uploaded on my site. I want to let people submit only .doc or .html or .pdf.... I have this code written in VBScript but there are some problems with it.

1. It doesn't seem to work even if I attach a .doc..
it keeps giving me that I have to still attach a file... 2.
if I have an extra
Code:
If Ext <> ".doc" OR ".html" Then
statement it gives me a TYPE MISMATCH STRING ERROR... any advise will be greatly appreciated.. thanks a lot.

Code:
<!-- Option Explicit
	dim validation
	dim header
	header = "TEST"
	dim docFile, str4
	docFile = Document.form1.blob.Value
	intLong = Len(docFile)
	str4 = intLong - intLong + 4
	dim Ext
	Ext = Right(docFile,str4)
	
	Function form1_OnSubmit
	validation = True
	If (Document.form1.docName.Value) = "" OR (Document.form1.blob.Value) = "" Then
	MsgBox "Please enter required information and click Submit! Thank you", 8, Header
	validation = False
	End If
	
	If Ext <> ".doc" OR ".html" Then
	MsgBox "Please select a valid file", 8, Header
	validation = False
	End If 
	
	
	If validation = True then
	form1_OnSubmit =True
	
	Else 
	form1_OnSubmit = False
	End If
	End Function
	-->
 
Try this change:
Code:
<!-- Option Explicit
    dim validation
    dim header
    header = "TEST"
    dim docFile, str4
    docFile = Document.form1.blob.Value
    intLong = Len(docFile)
    str4 = intLong - intLong + 4
    dim Ext
    Ext = Right(docFile,str4)
    
    Function form1_OnSubmit
    validation = True
    If (Document.form1.docName.Value) = "" OR (Document.form1.blob.Value) = "" Then
    MsgBox "Please enter required information and click Submit! Thank you", 8, Header
    validation = False
    End If
    
    If Ext <> ".doc" OR [red]Ext <> [/red]".html" Then
    MsgBox "Please select a valid file", 8, Header
    validation = False
    End If 
    
    
    If validation = True then
    form1_OnSubmit =True
    
    Else 
    form1_OnSubmit = False
    End If
    End Function
    -->

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hi thanks for the prompt posting... With this... it doesn't give the Ttpe mismatch error any more but it still doesn't submit anything... it just keeps giving me enter valid file msgbox... even if I have a .doc file. Any ideas?

Thanks
A.
 
Hello aspnewbee,

len(".html")=5 Hence str4 is good for .doc but not good enough for .html.

regards - tsuji
 
What if you change this section:
Code:
intLong = Len(docFile)
    str4 = intLong - intLong + 4
    dim Ext
    Ext = Right(docFile,str4)
to
Code:
    intLong = Len(docFile)
    str4 = intLong - InStr(dcoFile, ".")
    dim Ext
    Ext = Right(docFile,str4)


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hi tsuji... sorry I overlooked that man... stupid error... but even after I change it to "html" instead of ".html" it pops up the msgbox if file validity... I'm guessing its not getting docFile = Document.form1.blob.Value.... but I'll try to see if there is some other problem...

 
Hi Tom... I changed the code to what you put... but still the same problem...
thanks for your patience all...
 
Put in a message box to see what ext is being populated to.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
It not populating anything it the msgbox... so I'm guess its not picking up anything for docFile.

Code:
docFile = Document.form1.blob.Value

is not working... any ideas?

 
aspnewbee,

Correct a typo would work fine:
[tt] str4 = intLong - InStr(d[red]oc[/red]File, ".")[/tt]

- tsuji
 
tsuji.. I saw that and corrected it before testing... but still the same... doesn't go ahead and submit it.. it keeps giving me valid file msgbox... so I put a msgbox to see what its grabbing for docFile.. and its empty... so that means my statement docFile = Document.form1.blob.Value is not working for some reason...

A.
 
To help with that, we will probably need to see the code for the rest of the page. Or at least the pertinent code for the blob control.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hi Tom... there is nothing much on that page... I was just testing it... however here is the entire code...
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]

<html>
<head>
<title>ISPR</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="test_upload.asp" method="post" enctype="multipart/form-data" name="form1">
  <p>
    <input name="docName" type="text" id="docName" size="20">
</p>
  <p>
    <input name="blob" type="file" id="blob" size="20">
  </p>
  <p>
    <input type="submit" value="Submit">
  </p>
</form>
</body>
</html>
<script LANGUAGE="VBScript">
<!-- Option Explicit
    dim validation
    dim header
    header = "TEST"
    dim docFile, str4
    docFile = Document.form1.blob.Value
    
    'new code
    intLong = Len(docFile.Value)
    str4 = intLong - InStr(docFile, ".")
    dim Ext
    Ext = Right(docFile,str4)
	'new code end
    
    'intLong = Len(docFile)
    'str4 = intLong - intLong + 4
    'dim Ext
    'Ext = Right(docFile,str4)
    
    Function form1_OnSubmit
    validation = True
    If (Document.form1.docName.Value) = "" OR (Document.form1.blob.Value) = "" Then
    MsgBox "Please enter required information and click Submit! Thank you", 8, Header
    validation = False
    End If
    
    If Ext <> ".doc" OR Ext <> ".html" Then
    'MsgBox Ext
    MsgBox "Please select a valid file", 8, Header
    validation = False
    End If 
    
    
    If validation = True then
    form1_OnSubmit =True
    
    Else 
    form1_OnSubmit = False
    End If
    End Function
    -->

</script>
 
The problem is that you set Ext when the page loads. At that time it is nothing. You need to set it in the submit function and validate it there.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
aspnewbee,

There are quite a few you've to change in order to make it beginning to function. Here is a rough rundown.

[1] You've to move the whole scriptlines above function declaration. Move them into the function itself.
[2] The line
[tt]intLong = Len(docFile.Value)[/tt]
should be read
[tt]intLong = Len(docFile)[/tt]
because docFile is already a string by definition somewhere above it.
[3] Since "." is included in the conditional checking, you have to add one more place to the position. Also, location "." backward to accommodate multiple-dot filename.
[tt]str4 = intLong - InStrRev(docFile, ".")-1[/tt]
[4] The conditional should accommodate ".htm" as well which is not uncommon. Furthermore, if you use excluding type of condition using "<>", then I think it is the "and" rather than "or" you should use.
[tt]Ext = lcase(Right(docFile,str4))
If Ext <> ".doc" and Ext <> ".html" and Ext <> ".htm"Then
'MsgBox Ext
MsgBox "Please select a valid file", 8, Header
validation = False
End If[/tt]
[5] More difficult correction is to not allowing a submit button for the need of validation client-side scripting. Better to change the submit button to "button"
[tt]<input type="button" value="Submit" onclick="validate">[/tt]
[5.a] with the function itself changed to name:
[tt]function validate[/tt]
rather than
[tt]function form1_onSubmit[/tt]
and eliminate the use of form1_onSubmit inside the function.
[5.b] and inside the function after all the validation, submit the form1 by some device like:
[tt]if validation = true then
Document.form1.submit
exit function
end if[/tt]
[6] Another implementation is to disabling the "enter" key stroke for entering textbox and for file selection. You can use something like this:
[tt]sub proc_keycode
if window.event.keyCode=13 then
window.event.returnValue=false
exit sub
end if
end sub[/tt]
and screen the keydown event:
[tt]<input name="docName" type="text" id="docName" size="20" onkeydown="proc_keycode">[/tt]
and
[tt]<input name="blob" type="file" id="blob" size="20" onkeydown="proc_keycode">[/tt]

So, see quite a few corrections needed.

- tsuji

 
Correction:

The corresponding line should be read:
[tt]str4 = intLong - InStrRev(docFile, ".")[red]+[/red]1[/tt]

- tsuji
 
Hi tsuji.. thanks a lot... that was a lot of changes... I'll make the changes and update you on that. Thanks again..

A.
 
aspnewbee,

This is what it would be looked like.
Code:
<!-- Have to commented out this server-side directive
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]

<html>
<head>
<title>ISPR</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script LANGUAGE=VBScript>
<!--
sub proc_keycode
    if window.event.keyCode=13 then
        window.event.returnValue=false
    end if
end sub
Function validate_onclick()
    dim header
    dim docFile, str4
    dim Ext

    header = "TEST"
    docFile = Document.form1.blob.Value
    intLong = Len(docFile)
    str4 = intLong - InStrRev(docFile, ".")+1
    Ext = lcase(Right(docFile,str4))

    If (Document.form1.docName.Value = "") OR (Document.form1.blob.Value = "") Then
        MsgBox "Please enter required information and click Submit! Thank you", 8, Header
        document.form1.docName.focus
        exit function
    else
        If ((Ext <> ".doc") and (Ext <> ".html") and (Ext <> ".htm")) Then
            MsgBox "Please select a valid file", 8, Header
            document.form1.docName.focus
            exit function
        end if
    End If 
    document.form1.submit
End Function
-->
</script>
</head>
<body onload="document.form1.docName.focus">
<form action="test_upload.asp" method="post" enctype="multipart/form-data" name="form1">
<p>
    <input name="docName" type="text" id="docName" size="20" onkeydown="proc_keycode">
</p>
<p>
    <input name="blob" type="file" id="blob" size="20" onkeydown="proc_keycode">
</p>
<p>
    <input type="button" name="validate" value="Submit">
</p>
</form>
</body>
</html>
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top