Click here to Skip to main content
11,485,334 members (78,302 online)
Click here to Skip to main content

Class to check Strings for invalid characters

, 4 Nov 2000 79.9K 881 12
Rate this:
Please Sign up or sign in to vote.
An MFC Class which offers you the abbility to check Strings for invalid characters
<!-- Download Links -->
  • Download source files - 4 Kb
  • Download demo project - 15 Kb
  • <!-- Main HTML starts here -->

    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

    Share

    About the Author

    Thomas Hauth

    Germany Germany
    No Biography provided

    Comments and Discussions

     
    GeneralAmmendment for removing invalid chars Pin
    MrLeeGriffiths9-Dec-03 6:47
    memberMrLeeGriffiths9-Dec-03 6:47 
    GeneralRe: Ammendment for removing invalid chars Pin
    shaitan18@yahoo.com5-Jan-04 11:18
    membershaitan18@yahoo.com5-Jan-04 11:18 
    GeneralRe: Ammendment for removing invalid chars Pin
    Thim6-Oct-04 14:04
    memberThim6-Oct-04 14:04 
    GeneralCan't download ZIP. Pin
    Eugene Pustovoyt9-Nov-00 19:51
    memberEugene Pustovoyt9-Nov-00 19:51 
    GeneralRe: Can't download ZIP. Pin
    Chris Maunder1-Dec-00 12:52
    memberChris Maunder1-Dec-00 12:52 
    GeneralWhy don't you... Pin
    Thomas Freudenberg6-Nov-00 9:12
    memberThomas Freudenberg6-Nov-00 9:12 
    GeneralRe: Why don't you... Pin
    Andrew Peace6-Nov-00 14:24
    memberAndrew Peace6-Nov-00 14:24 
    GeneralRe: Why don't you... Pin
    Thomas Freudenberg6-Nov-00 22:46
    memberThomas Freudenberg6-Nov-00 22:46 
    GeneralRe: Why don't you... Pin
    Thomas Hauth7-Nov-00 0:41
    memberThomas Hauth7-Nov-00 0:41 

    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.

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