Welcome visitor
* Login
* Register
It is currently September 15th, 2019, 7:52 pm
All times are UTC [ DST ]
* Search
View unanswered posts - View active topics

FORUMS » Client Side » JavaScript



Post new topic Reply to topic  [ 3 posts ] 
Author Message
  validating file extension
PostPosted: March 4th, 2009, 1:40 am 
User avatar

Posts: 42
I have a form to upload a file, but only txt files. What can i do to know if it is a txt file?

thanks.


Top
 Profile  
 
  Re: validating file extension
PostPosted: March 4th, 2009, 2:02 am 
User avatar

Posts: 44
This may seem to be xl but it does many things.
1. Stores the file name on a single variable
2. It looks for ".", to make sure that a file name has been typed
3. Then it stores what seems to be an extension
3. Then, to make sure that it is the extension, it validates that there is 3 characters after the "."
4. If the extension is not txt it would be deleted automatically until txt is typed in
Maybe some parts are useless for you or something may be missing, but i hope this helps a bit.
<html>
<head>
<body>
<script type="text/javascript">
function validate(){
var string = document.form.field.value;
if(string.lastIndexOf(".")>=0){
    var last = string.lastIndexOf(".");
    var ext = string.substr(string.lastIndexOf(".")+1)
    if (ext.length==3){
        if (ext!= "txt"){
            document.form.field.value = string.slice(0,string.lastIndexOf(".")+1);
        }
    }
}}
</script>
</head>
<body>
<form name="form">
<input type="text" name="field" onkeyup="validate()">
</form>
</body>
</html>


Top
 Profile  
 
  Re: validating file extension
PostPosted: March 4th, 2009, 2:13 am 
User avatar

Posts: 42
thats fine man dont worry, thank you very much :mrgreen:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

FORUMS » Client Side » JavaScript


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron