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 ;-]
 
Actually, the script does not function in Netscape 4. It gives the following error:

"form is not defined."

Ryan ;-]
 
umm, you need to have your form named form. i don't see any form tag in your code, and i dont know coldfusion, so, you need to find a way to get what coldfusion outputs to be named form. adam@aauser.com
 
The name is already set to 'form'

What now? It's always Netscape that messes things up.

Ryan ;-]
 
can you post the code that is outputted... without the coldfusion tags. adam@aauser.com
 
Sure thing, here is the code (and note that I have set the event to onClick with the Submit button, because I am using ColdFusion to generate some client-side validation for some form fields, and it uses the onSubmit event):

------------------------------------------
[tt]<!--Set client management and application scope variables -->

<HTML>
<HEAD>

<META HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html; charset=iso-8859-1&quot;>
<!--Check for password in URL -->

<!--Authorization check -->

<!--Run query to get user information -->


<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 = form.File.value;
arr = crap.split(&quot;.&quot;);
reg = /^((txt)|(zip)|(doc)|(pdf)|(ppt)|(htm)|(html)|(xls))$/;
if(reg.test(arr[arr.length - 1]))
{

}
else
{
alert(&quot;You can only upload Adobe PDF, HTML, Microsoft Access, Microsoft Excel, Microsoft PowerPoint, Microsoft Word, Plain Text, and ZIP files. Please select another file.&quot;);
return false;
}
}

//-->
</script>


<script LANGUAGE=JAVASCRIPT TYPE=&quot;text/javascript&quot; >

<!--


function _CF_onError(form_object, input_object, object_value, error_message)
{
alert(error_message);
return false;
}



function _CF_hasValue(obj, obj_type)
{
if (obj_type == &quot;TEXT&quot; || obj_type == &quot;PASSWORD&quot;)
{
if (obj.value.length == 0)
return false;
else
return true;
}
else if (obj_type == &quot;SELECT&quot;)
{
for (i=0; i < obj.length; i++)
{
if (obj.options.selected)
return true;
}

return false;
}
else if (obj_type == &quot;SINGLE_VALUE_RADIO&quot; || obj_type == &quot;SINGLE_VALUE_CHECKBOX&quot;)
{

if (obj.checked)
return true;
else
return false;
}
else if (obj_type == &quot;RADIO&quot; || obj_type == &quot;CHECKBOX&quot;)
{

for (i=0; i < obj.length; i++)
{
if (obj.checked)
return true;
}

return false;
}
}


function _CF_checkform(_CF_this)

{

if (!_CF_hasValue(_CF_this.Resource_Title, &quot;TEXT&quot; ))

{

if (!_CF_onError(_CF_this, _CF_this.Resource_Title, _CF_this.Resource_Title.value, &quot;Please enter a title for your resource.&quot;))

{

return false;

}

}


return true;

}


//-->

</script>

</HEAD>


<BODY BGCOLOR=&quot;#FFFFFF&quot;>
<DIV ALIGN=&quot;CENTER&quot;>
<P><IMG SRC=&quot; <FONT FACE=&quot;Arial, Helvetica, sans-serif&quot;><I><B><U><FONT SIZE=&quot;4&quot;>Lynbrook Public School District</FONT></U></B></I></FONT></P>
<br>
</DIV>

<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>
<FORM NAME=&quot;form&quot; ACTION=&quot;add.cfm&quot; METHOD=POST onSubmit=&quot;return _CF_checkform(this)&quot; ENCTYPE=&quot;multipart/form-data&quot;>
<input type=&quot;hidden&quot; name=&quot;Teacher&quot; value=&quot;1&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;>
<INPUT TYPE=&quot;TEXT&quot; NAME=&quot;Resource_Title&quot; VALUE=&quot;&quot; SIZE=&quot;10&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>
<OPTION VALUE=&quot;ZIP&quot;>ZIP</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;return stupids()&quot;>
<INPUT TYPE=&quot;RESET&quot; NAME=&quot;Reset&quot; VALUE=&quot;Reset&quot;>
</FONT></TD>
</TR>
</TABLE>
</FORM>
<!--Run query to get user information -->


<DIV ALIGN=&quot;CENTER&quot;>
<TABLE BORDER=&quot;0&quot;>
<TR>
<TD><FONT SIZE=&quot;2&quot; FACE=&quot;Arial, Helvetica, sans-serif&quot;>&amp;copy;2000, <I>ZEAL</I>
network, INC.</FONT> </TD>
<TD><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><A HREF=&quot;logout.cfm&quot;><B><I>LOGOUT</I></B></A></FONT></TD>
</TR>
</TABLE>
</DIV>



</BODY>
</HTML>
[/tt]

------------------------------------------


Ryan ;-]
 
lucid,

why doesn't the above code work in netscape?

Ryan ;-]
 
maybe its getting submitted anyway, try changing you onsubmit to:

onSubmit=&quot;return _CF_checkform(this);return stupids();&quot;

and take it out of the submit button. adam@aauser.com
 
In Netscape 6, the function doesn't seem to be processed and in Netscape 4 it gives the following error:

&quot;form is not defined&quot;

Lucid's suggestion didn't work either.

Ryan ;-]
 
in function stupids, change form.File.value to:

document.form.File.value;
jared@aauser.com
 
Thanks so much! There's a reason you and Lucid are the top guys around here.

Ryan ;-]
 
I have a similiar question:
I want to validate a file formfield so user can only upload files which filename is no longer than 20 chars (include its extension)

For example if they upload this file &quot;C:\Program Files\Macromedia\Dreamweaver 4\ResourcesOfInterest.htm&quot; it will rejected but not &quot;C:\Program Files\Macromedia\Dreamweaver 4\License.htm&quot;

Djon
 
I am again adapting this code, but there is a problem with this line (frm is the name of the form):

crap = document.frm.upfile.value;

JS says that the object is null or does not exist.

Ryan ;-]
 
Nevermind, guys!

Thanks.

Ryan ;-]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top