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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate File Formfield 3

Status
Not open for further replies.

rmz8

Programmer
Aug 24, 2000
210
0
0
US
Is there any way to create a script that checks the last three or four characters of a form field to make sure that they meet the criteria. For example, I am setting up a form where people can upload files, but I only want certain file extensions to be allowed. How can I make it so that, let's say, "doc" and "html" and "xls" are the only permissible extensions and if another extension is encountered an alert pops up?

Ryan ;-]
 
<input type=&quot;file&quot; id=&quot;unf&quot; onchange=&quot;stupids()&quot;>
<script>
function stupids()
{
crap = unf.value;
arr = crap.split(&quot;.&quot;);
reg = /[txt]|
HTML:
|[xls]/;
	if(reg.test(arr[1]))
	{
		alert(&quot;good&quot;)
	}
	else
	{
		alert(&quot;bad&quot;);
	}
}
</script>

this works for me adam@aauser.com
 
didn't work on IE5.5 for me jared@aauser.com
 
well, it works for me. maybe you should test it in your browser instead of homesite, jared. adam@aauser.com
 
well, I was able to upload exe's even testing in my browser (IE5.5) instead of homesite...their might be something wrong with the regular expression you are using jared@aauser.com
 
ya, alright, fine:

reg = /^([txt]|
HTML:
|[xls])$/;

that should fix it. adam@aauser.com
 
ok this one does:

reg = /^((txt)|(html)|(xls))$/; adam@aauser.com
 
and that won't always work - if a file name is:

party.htm.exe

you should use:

arr = crap.substring(crap.lastIndexOf(&quot;.&quot;)+1,crap.length) jared@aauser.com
 
and change the if to say:

reg.test(arr) jared@aauser.com
 
or you could use mine and change:

if(reg.test(arr[1]))

to

if(reg.test(arr[arr.length - 1])) adam@aauser.com
 
Hehe, so what is the completed code--you guys made a lot of revisions!

Ryan ;-]
 
<input type=&quot;file&quot; id=&quot;unf&quot; onchange=&quot;stupids()&quot;>
<script>
function stupids()
{
crap = unf.value;
arr = crap.split(&quot;.&quot;);
reg = /^((txt)|(html)|(xls))$/;
if(reg.test(arr[arr.length - 1]))
{
alert(&quot;good&quot;)
}
else
{
alert(&quot;bad&quot;);
}
}
</script> adam@aauser.com
 
I set that up and call to it onClick of the Submit button, but I get &quot;unf&quot; not defined. Note that I set the ELSE action blank so that it does nothing (that shouldn't make any difference). Here is the entire code of the page (excuse the ColdFusion):

========================================================

<CFOUTPUT>
<HTML>
<HEAD>

<META HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html; charset=iso-8859-1&quot;>
<!--Check for password in URL -->
<cfif IsDefined(&quot;Session.PWord&quot;) AND IsDefined(&quot;Session.UN&quot;)>
<!--Authorization check -->
<CFIF IsDefined(&quot;Session.Auth&quot;) is NOT TRUE>
<CFLOCATION URL=&quot;login.cfm&quot;>
<CFELSE>
<!--Run query to get user information -->
<cfquery datasource=&quot;#DSN#&quot; name=&quot;getUser&quot;>
SELECT RealName FROM Users WHERE (UserName = '#Session.UN#' AND Password = '#Session.PWord#')
</cfquery>
<cfif getuser.recordcount EQ &quot;0&quot;>
<cflocation url=&quot;login.cfm&quot;>
<cfelse>
<TITLE>ZEAL network's GATSBY: Add Resource</TITLE>
<script language=&quot;JavaScript&quot;>
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}

function addCode(mode,form)
{
if (mode == &quot;BOLD&quot;)
{
form.Resource_Description.value += &quot;<b></b>&quot;;
}
else if (mode == &quot;BREAK&quot;)
{
form.Resource_Description.value += &quot;<br>&quot;;
}
}

function stupids()
{
crap = unf.value;
arr = crap.split(&quot;.&quot;);
reg = /^((txt)|(html)|(xls))$/;
if(reg.test(arr[arr.length - 1]))
{
alert(&quot;good&quot;)
}
else
{

}
}

//-->
</script>
</HEAD>


<BODY BGCOLOR=&quot;##FFFFFF&quot;><cfinclude template=&quot;../includes/header.html&quot;>
<H2><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot;><U>Add Resource</U> <B><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;></FONT></B><U><B><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;>[<A HREF=&quot;index.cfm&quot;>Go
back</A>] </FONT></B></U></FONT></H2>
<cfform action=&quot;add.cfm&quot; method=&quot;post&quot; name=&quot;form&quot; enctype=&quot;multipart/form-data&quot;>
<input type=&quot;hidden&quot; name=&quot;Teacher&quot; value=&quot;#Session.Teacher#&quot; size=&quot;5&quot; maxlength=&quot;30&quot;>
<TABLE BORDER=&quot;0&quot;>
<TR>
<TD HEIGHT=&quot;7&quot; WIDTH=&quot;1%&quot; VALIGN=&quot;TOP&quot;><I><B><FONT SIZE=&quot;2&quot; FACE=&quot;Arial, Helvetica, sans-serif&quot;><A HREF=&quot;##&quot; onClick=&quot;MM_openBrWindow('../help/resource_title.html','','scrollbars=yes,width=200,height=200')&quot;>?</A></FONT></B></I></TD>
<TD HEIGHT=&quot;7&quot; WIDTH=&quot;25%&quot; VALIGN=&quot;TOP&quot;><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><B>Resource
Title:</B></FONT></TD>
<TD HEIGHT=&quot;7&quot; WIDTH=&quot;74%&quot;> <FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;>
<CFINPUT TYPE=&quot;TEXT&quot; NAME=&quot;Resource_Title&quot; VALUE=&quot;&quot; SIZE=&quot;10&quot; REQUIRED=&quot;yes&quot; MESSAGE=&quot;Please enter a title for your resource.&quot;>
</FONT></TD>
</TR>
<TR>
<TD HEIGHT=&quot;7&quot; WIDTH=&quot;1%&quot; VALIGN=&quot;TOP&quot;><I><B><FONT SIZE=&quot;2&quot; FACE=&quot;Arial, Helvetica, sans-serif&quot;><A HREF=&quot;##&quot; onClick=&quot;MM_openBrWindow('../help/resource_type.html','','scrollbars=yes,width=200,height=200')&quot;>?</A></FONT></B></I></TD>
<TD HEIGHT=&quot;7&quot; WIDTH=&quot;25%&quot; VALIGN=&quot;TOP&quot;><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><B>Resource
Type:</B></FONT></TD>
<TD HEIGHT=&quot;7&quot; WIDTH=&quot;74%&quot;> <FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;>
<SELECT NAME=&quot;Resource_Type&quot;>
<OPTION VALUE=&quot;Adobe PDF&quot;>Adobe PDF</OPTION>
<OPTION VALUE=&quot;HTML&quot;>HTML</OPTION>
<OPTION VALUE=&quot;Microsoft Access&quot;>Microsoft Access</OPTION>
<OPTION VALUE=&quot;Microsoft Excel&quot;>Microsoft Excel</OPTION>
<OPTION VALUE=&quot;Microsoft PowerPoint&quot;>Microsoft PowerPoint</OPTION>
<OPTION VALUE=&quot;Microsoft Word&quot; SELECTED>Microsoft Word</OPTION>
<OPTION VALUE=&quot;Plain Text&quot;>Plain Text</OPTION>
</SELECT>
</FONT></TD>
</TR>
<TR>
<TD HEIGHT=&quot;2&quot; WIDTH=&quot;1%&quot; VALIGN=&quot;TOP&quot;><I><B><FONT SIZE=&quot;2&quot; FACE=&quot;Arial, Helvetica, sans-serif&quot;><A HREF=&quot;##&quot; onClick=&quot;MM_openBrWindow('../help/resource_description.html','','scrollbars=yes,width=200,height=200')&quot;>?</A></FONT></B></I></TD>
<TD HEIGHT=&quot;2&quot; WIDTH=&quot;25%&quot; VALIGN=&quot;TOP&quot;>
<P><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><B>Resource Description:</B></FONT>
<INPUT TYPE=&quot;button&quot; VALUE=&quot;Line Break&quot; onClick=&quot;addCode('BREAK',document.form)&quot; NAME=&quot;button&quot;>
<INPUT TYPE=&quot;button&quot; VALUE=&quot;Bold&quot; onClick=&quot;addCode('BOLD',document.form)&quot; NAME=&quot;button&quot;>
<BR>
</P>
</TD>
<TD HEIGHT=&quot;2&quot; WIDTH=&quot;74%&quot;> <FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;>
<TEXTAREA NAME=&quot;Resource_Description&quot; COLS=&quot;70&quot; ROWS=&quot;8&quot; REQUIRED=&quot;Yes&quot; MESSAGE=&quot;Please enter a valid description for your resource.&quot;></TEXTAREA>
</FONT></TD>
</TR>
<TR>
<TD HEIGHT=&quot;2&quot; WIDTH=&quot;1%&quot; VALIGN=&quot;TOP&quot;><I><B><FONT SIZE=&quot;2&quot; FACE=&quot;Arial, Helvetica, sans-serif&quot;><A HREF=&quot;##&quot; onClick=&quot;MM_openBrWindow('../help/file.html','','scrollbars=yes,width=200,height=200')&quot;>?</A></FONT></B></I></TD>
<TD HEIGHT=&quot;2&quot; WIDTH=&quot;25%&quot; VALIGN=&quot;TOP&quot;><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><B>File:</B></FONT></TD>
<TD HEIGHT=&quot;2&quot; WIDTH=&quot;74%&quot;>
<INPUT TYPE=&quot;file&quot; id=&quot;unf&quot; NAME=&quot;File&quot;>
</TD>
</TR>
<TR>
<TD HEIGHT=&quot;2&quot; COLSPAN=&quot;3&quot;> <FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;>
<INPUT TYPE=&quot;submit&quot; NAME=&quot;Submit&quot; VALUE=&quot;Submit&quot; onClick=&quot;stupids()&quot;>
<INPUT TYPE=&quot;RESET&quot; NAME=&quot;Reset&quot; VALUE=&quot;Reset&quot;>
</FONT></TD>
</TR>
</TABLE>
</cfform><cfinclude template=&quot;../includes/footer.html&quot;></CFIF></cfif>
<CFELSE>
<CFLOCATION URL=&quot;login.cfm&quot;>
</CFIF>
</BODY>
</HTML>
</CFOUTPUT>

========================================================

Ryan ;-]
 
the unf.value part will not work in netscape... you will probably have to use document.formname.fieldname.value instead. adam@aauser.com
 
But the code above didn't even work in IE 5.5.

Ryan ;-]
 
It's on my local server.

Ryan ;-]
 
try changing it from unf.value to form.File.value and you also may want to instead of taking out the alert that told you if it was bad, you may want to take out the one that says good, and put the bad one back. so, if the file extension is right it will submit, otherwise it will alert. you may also want to change the function call from onclick to an onsubmit in the form tag... and right after the alert to tell them that the extension is not acceptable, put:

return false;

and your onsubmit should look like this:

onsubmit=&quot;return stupids()&quot;


adam@aauser.com
 
Thanks so much! It works great.

Ryan ;-]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top