Script code:
<script type="text/javascript">function check_upload(upload_field){ var re_text = /\.txt|\.pdf|\.zip/i; var filename = upload_field.value;
/* Checking file type */ if (filename.search(re_text) == -1) { alert("File does not have (txt / pdf / zip) extension"); upload_field.form.reset(); return false; }return true; } function verify_upload(){ if(document.upload_file.file1.value == "") { alert("Please select a file"); return false; }return true; }</script>
Form code:
<form name="upload_file" action="" method="post" enctype="multipart/form-data"><input type="file" name="file1" id="file" onChange="check_upload(this)"><input type="submit" name="submit" value="Upload" onclick="return verify_upload()" /></form>
<script type="text/javascript">function check_upload(upload_field){ var re_text = /\.txt|\.pdf|\.zip/i; var filename = upload_field.value;
/* Checking file type */ if (filename.search(re_text) == -1) { alert("File does not have (txt / pdf / zip) extension"); upload_field.form.reset(); return false; }return true; } function verify_upload(){ if(document.upload_file.file1.value == "") { alert("Please select a file"); return false; }return true; }</script>
Form code:
<form name="upload_file" action="" method="post" enctype="multipart/form-data"><input type="file" name="file1" id="file" onChange="check_upload(this)"><input type="submit" name="submit" value="Upload" onclick="return verify_upload()" /></form>
Note: This script ensures whether the chosen file is of type txt, pdf or zip.
ReplyDelete