View Full Version : How to check spaces when upload file?
buckbeak
09-19-2002, 05:35 AM
How can I make sure that the file upload does not contain spaces?see my codes below:
<input type="file" name="img" size= "35" onBlur="return
check_space(this.value)">
<script language="javascript">
function check_space(str){
var invalid = " ";
if (str.value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed.");
return false;}
return true;
}
</script>
Can someone modify this code for me? or any better suggestions on how to do this?
glenngv
09-19-2002, 07:22 AM
<input type="file" name="img" size= "35" onBlur="return
check_space(this)">
<script language="javascript">
function check_space(strObj){
if (strObj.value.indexOf(" ") > -1) {
alert("Sorry, spaces are not allowed.");
strObj.focus();
return false;
}
return true;
}
</script>
buckbeak
09-19-2002, 07:41 AM
hi glenn,
nothing happen....is it because <input type="file"> tag cannot use onBlur function?
glenngv
09-19-2002, 08:01 AM
it works for me. i choose a file with space in the filename and the alert displays. i use IE5.5
buckbeak
09-19-2002, 10:48 AM
hi glen,
sorry it's my mistake. The js works very well. But I still haven't solve my problem.
If the file upload is c:/My Documents/test.html, the alert(Sorry .....) will appear because there is space between My and Document. How can I just check the file upload and not the whole path?? Is that possible???
glenngv
09-19-2002, 11:00 AM
<script language="javascript">
function check_space(strObj){
bslash=strObj.value.lastIndexOf("\\");
if (bslash==-1){
alert("Invalid path.");
strObj.focus();
return false;
}
else if (strObj.value.substring(bslash+1).indexOf(" ") > -1) {
alert("Sorry, spaces are not allowed.");
strObj.focus();
return false;
}
return true;
}
</script>
Spookster
09-19-2002, 11:21 AM
Instead of working so hard to check for spaces and prevent the upload using javascript and ultimately irritating the end-user why don't you just accept the spaces and use whatever server-side language you are using to replace the spaces with underscores when it is uploaded. There is also no guarantee that the end-user has javascript enabled anyways.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.