I take a file via a form , and ask to validate its type. but it sounds the java script doesn’t work or the invoke is not seen ?
<form enctype="multipart/form-data" action="Upload2.jsp"
method="post" onsubmit="javascript:return validate();">
<td><b>Choose txt file To Upload:</b></td>
<td><input name="file" type="file"></td>
<td><input type="submit" value="Upload File" ></td>
</form>
And here’s the script:
<script language="JavaScript">
function validate(){
if(document.form.file.value == "" ) {
alert("Enter course code !");
return false;}
else {
var value = document.form.file.value;
var ext = value.substr(dot, value.length);
//ext = value.slice(file.indexOf(".")).toLowerCase();
if ( ext=="txt" ) {
return true; }
else{
alert("choose a .txt file");
return false;}
}
}
</script>
And this is the form..
tho i am not sure of my script correctness?
You can capture what’s after the last period in a string using:
You take your String
valuesplit it into an array:now myArray is:
You can now pop the last value from the array using
.pop().now
in context:
If you change your onsubmit to:
Then you will get the form element as first argument in value:
NOTE:
Never use javaScript as your only validation. Use some sort of server-site language to determinate if the validation is passed.
Use JS to make i more slick for the user to operate on your site.