How do I detect invalid characters in a string?

Can anyone help me with detecting characters such as “/ \ ? % * : | " < >” in a string. For example, when I type “filename*”, that would be invalid.

HOWSHI
there are many ways to skin a cat, so here is one of them:


//loop around and check for an invalid character in a string
//valid ASCII characters are 0-9, A-Z and a-z
//decimal character codes are: 48-57, 65-90 and 97-122 respectively

//get the string length to check
nStringLength=GetStringLength(sStringToCheck);

nInvalidCharFound=0;  //set true if we find an invalid char so we can bail asap
nIndex=0; //counter so we can loop through
nChar=0;  //character at nIndex position

repeat
  nChar=GetNthCharacter(sStringToCheck,nIndex); //get the character at index number
  IncrementVariable(nIndex);  //increment the index
  //check to see if all 3 statements are valid. if the char is not within the limits specified then its invalid.
  //add more as you see fit, even make the statics variables!!
  if (
    (not(IsWithinLimits(nChar,48,57)))  //0-9
    and
    (not(IsWithinLimits(nChar,65,90)))  //A-Z
    and
    (not(IsWithinLimits(nChar,97,122))) //a-z
    ) then 
    nInvalidCharFound=nIndex-1; //set the invalidchar variable to the position it was found
  endif
  
until ((nInvalidCharFound<>0) or (nIndex==nStringLength));  //bail if we get an invalid char found or we have reached the end of the string


sStringToCheck is the string you want to check and nInvalidCharFound is the position the invalid char was found. If this variable is 0 after the code block then no invalid characters were found.

you can add other character ranges if you like or extend the ones that are there.

Hmmm, looks like some good code to enter in to the competition!!:stuck_out_tongue:

I hope this helps

Nick

Thank you so much. I tried another way, and it worked!! I used the action command FindCharacterinString instead.

HOWSHI
Nice one!! Ah… the beauty of Opto… its just so good it allows you figure out your own answers!!:mad::(:):D:cool: