Click here to Skip to main content
15,888,733 members
Articles / Desktop Programming / MFC
Article

Class to check Strings for invalid characters

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
4 Nov 2000 97.2K   958   11   9
An MFC Class which offers you the abbility to check Strings for invalid characters
  • Download source files - 4 Kb
  • Download demo project - 15 Kb
  • Introduction

    Some time ago, I wrote an application which allows the user to type in his name. To prevent the user from typing "dirty" characters like §)"§)"=$ I wrote the CCharCheck class.

    You can tell this class exactly which characters are allowed. Then, CCharCheck checks your strings for invalid chars and tells you about it.

    You add single chars by calling AddChar(...). The following piece of code adds the character 'a':

    // Adding the character 'a'
    myCharCheck.AddChar( 'a' )

    You can also add ranges to the list of allowed characters. For example you want to add all which lie in the ASCII table between a and z. So you call AddRange( 'a', 'z' ). All characters between a and z are added. NOTE: the 2 chars you give (in our example a and z) are also added. It is not possible to do this operation the other direction (AddRange('z', 'a')). NOTE: CCharCheck is case sensitive!

    For example if you want to add all capital and minor letters and all numbers, you would type:

    // add all minor and capital letters and all numbers
    	CharCheck.AddRange( 'a', 'z' );
    	CharCheck.AddRange( 'A', 'Z' );
    	CharCheck.AddChar( '0' );
    	CharCheck.AddChar( '1' );
    	CharCheck.AddChar( '2' );
    	CharCheck.AddChar( '3' );
    	CharCheck.AddChar( '4' );
    	CharCheck.AddChar( '5' );
    	CharCheck.AddChar( '6' );
    	CharCheck.AddChar( '7' );
    	CharCheck.AddChar( '8' );
    	CharCheck.AddChar( '9' );

    OK, now we've told the class which characters are allowed. Now the function CheckString is called to check a string for invalid parameters. The first paramter of CheckString is from type CString and it's the string you want to check. The second param gives you the ability to tell CheckString from which position in the string it should start checking. 2 means after the first character of the string. 0 means the whole string should be searched.
    If a disallowed char is found, CheckString returns the position of in the string (1 is at the first position). If nothing was found, 0 is returned.

    The function ResetRanges is used to delete all ranges and chars that were added. It's a full reset of the class.

    Planed improvements


    I want to add a function which just deletes the non-allowed chars from the string.

    Sample Code

    CCharCheck CharCheck;
    int ret;
    
    // add all minor letters and all numbers
    CharCheck.AddRange( 'a', 'z' );
    CharCheck.AddChar( '0' );
    CharCheck.AddChar( '1' );
    CharCheck.AddChar( '2' );
    CharCheck.AddChar( '3' );
    CharCheck.AddChar( '4' );
    CharCheck.AddChar( '5' );
    CharCheck.AddChar( '6' );
    CharCheck.AddChar( '7' );
    CharCheck.AddChar( '8' );
    CharCheck.AddChar( '9' );
    
    // check the string
    // if return value is bigger than 0, a not allowed char was found in the string
    ret = CharCheck.CheckString( "This is the string to check " , 0 );
    
    if (ret  > 0)
    {
        // string contains invalid char
    }
    else
    {
        // string OK
    }
    

    NOTE: This class is free, you can use it for whatever you want. But it would be nice if you send me an EMail and tell me about your experiences (ThomasHauth@gmx.de).

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Germany Germany
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralAmmendment for removing invalid chars Pin
    Griffter UK9-Dec-03 5:47
    Griffter UK9-Dec-03 5:47 
    Hi, I've supplied here below the ammendment you were going to make for removing the invalid characters from the passed string.

    Make the following change in the header file for the CheckString function declaration:

    <br />
    int CheckString( CString &strToCheck, long lBegin, bool RemoveDeniedChars=false);<br />


    And change the function in the CPP file to this

    <br />
    int CCharCheck::CheckString(CString &strToCheck, long lBegin, bool RemoveDeniedChars)<br />
    {<br />
    	<br />
    	long curPos        = 0;<br />
    	long RangesCounter = 0;<br />
    	bool CharOK        = true;<br />
    	<br />
    	if (lBegin < 1)<br />
    		lBegin = 1;<br />
    	<br />
    	//Go through each character in the input string<br />
    	for (curPos = lBegin; curPos <= strToCheck.GetLength(); curPos ++)<br />
    	{<br />
    		//Assume all is well until we find a disallowed character<br />
    		CharOK = true;<br />
    		<br />
    		//Check our ranges for a disallowed character<br />
    		for (RangesCounter = 0; RangesCounter <= Ranges.GetUpperBound(); RangesCounter ++)<br />
    		{<br />
    			if (Ranges[RangesCounter] == strToCheck.GetAt( curPos -1 ))<br />
    			{<br />
    				if (RemoveDeniedChars)<br />
    				{<br />
    					strToCheck.Remove(Ranges[RangesCounter]);<br />
    				}<br />
    <br />
    				CharOK = false;<br />
    			}<br />
    		}<br />
    <br />
    		//if we found a bad character and we don't want to remove it then return the position<br />
    		//that we found it at<br />
    		if ((!CharOK) && (!RemoveDeniedChars))<br />
    			return curPos;<br />
    <br />
    	}<br />
    <br />
    	return 0;<br />
    }<br />
    <br />


    The function will behave as it did before with no changes, but if you add the "true" flag to the checkstring function it will modify the passed CString to take out the invalid characters!

    Have fun, and let me know what you think.

    Kind regards,

    Lee Griffiths
    GeneralRe: Ammendment for removing invalid chars Pin
    shaitan18@yahoo.com5-Jan-04 10:18
    shaitan18@yahoo.com5-Jan-04 10:18 
    GeneralRe: Ammendment for removing invalid chars Pin
    Thimpat6-Oct-04 13:04
    Thimpat6-Oct-04 13:04 
    GeneralCan't download ZIP. Pin
    9-Nov-00 18:51
    suss9-Nov-00 18:51 
    GeneralRe: Can't download ZIP. Pin
    Chris Maunder1-Dec-00 11:52
    cofounderChris Maunder1-Dec-00 11:52 
    GeneralWhy don't you... Pin
    6-Nov-00 8:12
    suss6-Nov-00 8:12 
    GeneralRe: Why don't you... Pin
    Andrew Peace6-Nov-00 13:24
    Andrew Peace6-Nov-00 13:24 
    GeneralRe: Why don't you... Pin
    Thomas Freudenberg6-Nov-00 21:46
    Thomas Freudenberg6-Nov-00 21:46 
    GeneralRe: Why don't you... Pin
    Thomas Hauth6-Nov-00 23:41
    Thomas Hauth6-Nov-00 23:41 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.