Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unwanted characters warning.

Status
Not open for further replies.

struth

Programmer
Aug 26, 2001
114
0
0
GB
I trying to prevent the uploading of files which include any character of ther than 1-9 and a-z ... but it isn't quite working.

function checkUploadString(str) {
checkStr = FilenameOnly(str);
var ValidString = "01234567890abcdefghilklmnop qrstuvABCDEFGHIJKLMNOPQRSTUVWXYZ";
var Ret = allowInString(checkStr, ValidString);
if (Ret = true){
alert("Sorry can't upload this file /n Please rename it using only letters a-z and 0-9");
return(false);
}
}


function allowInString (InString, RefString) {
if(InString.length==0) return (false);
for (Count=0; Count < InString.length; Count++) {
TempChar= InString.substring (Count, Count+1);
if (RefString.indexOf (TempChar, 0)==-1)
return (false);
}
return (true);
}

function FilenameOnly (InString) {
LastSlash=InString.lastIndexOf ('\\', InString.length-1)
OutString=InString.substring (LastSlash+1, InString.length)
return (OutString);
}

Where am I going wrong?
TIA

Away from the actual ... everything is virtual
 
without looking too deep into your code, i noticed this:

Code:
if (Ret = true){

which should be:

Code:
if (Ret =[red]=[/red] true){

if you change that, does it work ok?



*cLFlaVA
----------------------------
[tt]( <P> <B>)[sup]13[/sup] * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks cLFlaVA ... I have made this mistake before but on this occasion, I've tried it and it makes no difference.

Away from the actual ... everything is virtual
 
Another thing - try using regular expressions when doing string validation. They'll cut your code down extensively. The function you wrote is an easy 1-liner with regexp, so there's no need to reinvent the wheel. Also, when checking for equality to a boolean value, you don't have to put the ==true part, it's implied. To do ==false you only have to add a ! to the beginning of the expression.
Code:
function checkUploadString(str) {
    checkStr = FilenameOnly(str);
    var Ret = [!]/[^A-Za-z0-9 ]/g.test(checkStr);[/!]
    if ([!]!Ret[/!]) {
       alert("Sorry can't upload this file /n Please rename it using only letters a-z and 0-9");
       return(false);
    }
}

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Thanks kaht. I was having problems getting your regexp to work so I did this little test thing. But still it did not work smoothly as I got not no alert on checkBad and a 'no problem' alert on testGood even if I introduced bad characters like 'ok%& doc.doc'. Have I got it wrong in removing the '!' before 'Ret' on testGood?

Code:
<HTML>
<HEAD>
<TITLE>Unwanted characters test Test</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function checkGood() {
    checkStr = 'C:\My documents\test docs\ok doc.doc';
    var Ret = /[^A-Za-z0-9 ]/g.test(checkStr);
    if (Ret) {
       alert("no problem");
       return(false);
    }
}

function checkBad() {
    checkStr = 'C:\My documents\test docs\bad%73$’#.doc';
    var Ret = /[^A-Za-z0-9 ]/g.test(checkStr);
    if (!Ret) {
       alert("Sorry can't upload this file \n Please rename it using only letters a-z and 0-9");
       return(false);
    }
}
</SCRIPT>
<BODY>
<FORM>
<INPUT TYPE="button" VALUE="Check good" onClick="checkGood()">
<INPUT TYPE="button" VALUE="Check bad" onClick="checkBad()">
</FORM>
</BODY>
</HTML>

Away from the actual ... everything is virtual
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top