Click here to Skip to main content
Licence 
First Posted 4 Nov 2000
Views 72,857
Bookmarked 12 times

Class to check Strings for invalid characters

By | 4 Nov 2000 | Article
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

    About the Author

    Thomas Hauth



    Germany Germany

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralAmmendment for removing invalid chars PinmemberMrLeeGriffiths5:47 9 Dec '03  
    GeneralRe: Ammendment for removing invalid chars Pinmembershaitan18@yahoo.com10:18 5 Jan '04  
    GeneralRe: Ammendment for removing invalid chars PinmemberThim13:04 6 Oct '04  
    GeneralCan't download ZIP. PinmemberEugene Pustovoyt18:51 9 Nov '00  
    GeneralRe: Can't download ZIP. PinmemberChris Maunder11:52 1 Dec '00  
    GeneralWhy don't you... PinmemberThomas Freudenberg8:12 6 Nov '00  
    GeneralRe: Why don't you... PinmemberAndrew Peace13:24 6 Nov '00  
    GeneralRe: Why don't you... PinmemberThomas Freudenberg21:46 6 Nov '00  
    GeneralRe: Why don't you... PinmemberThomas Hauth23:41 6 Nov '00  

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

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

    Permalink | Advertise | Privacy | Mobile
    Web04 | 2.5.120517.1 | Last Updated 5 Nov 2000
    Article Copyright 2000 by Thomas Hauth
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid