Click to See Complete Forum and Search --> : checking for extension


Jarod
10-05-2003, 06:13 PM
i have this code to detect an extension and im trying to make a alert if the file doesnt have an extension.

i dont think i have it written right



<script language="JavaScript"><!--

function getExtension(value) {

if(value.substring(value.lastIndexOf('.') + 1,value.length) == "")

{

alert("This file doesnt not seem to have a extension. This can cause problems. Please make sure your file has a extension.")

}

}

//--></script>

<form action="upload.cgi" method="post" enctype="multipart/form-data">



<input type="hidden" name="project" value="20">

<input type="hidden" name="filefolder" value="0DbWUJoBfSk">

File:<br>

<input type="File" name="FILE1">

<br>Description:<br>

<input type="text" name="description" size="50">

<br>Destination Folder:<br><select name ='folder'>


<option value =" 6">Data / Photos</option>





</select>

<br><br>



<input type="Submit" value="Upload File" onClick="alert(getExtension(this.form.myFile.value))">

pyro
10-05-2003, 06:32 PM
You could try something like this:

<script type="text/javascript">
function check() {
if (!/\./.test(document.myform.filename.value)) {
alert ("You must enter a file extention");
return false;
}
}
</script>
</head>
<body>
<form name="myform" action="" onsubmit="return check();">
<p><input type="text" name="filename">
<input type="submit" value="Submit"></p>
</form>

Charles
10-05-2003, 06:37 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
String.prototype.hasExtension = function () {return /\..+$/.test(this)}
// -->
</script>

<form action="">
<div>
<label>File<input type="file" onchange="if (!this.value.hasExtension()) {alert('We need a file name with an extension.'); this.value=''; this.focus()}"></label>
<button type="submit">Submit</button>
</div>
</form>

Jarod
10-05-2003, 06:38 PM
THANKS A LOT MAN

AdamBrill
10-05-2003, 08:43 PM
I just wanted to point out that this should be done server-side as well as client-side. Client-side is way too easy to break to totally rely on that...