Simple String Check Addition to C++ (console function usage) AlphaNumeric Test
by John "Mythic" Henry · in Torque 3D Professional · 11/29/2011 (8:20 am) · 0 replies
As I preffer C++ over script, (hard habit to break), and I Always check things..
Here's a simple addition to string functions (script access):
[ConsoleFunctions.cpp]
Just add it to the end of the file and recompile...
Usefull for checking Username and passwords. (I dont allow Sql injection).
There are probably simplier ways and can be done thru script, but C++ is faster.
Process:
call function with a string: %result = isValidAlNum( %username );
Result:
true -> if it contains [a-z][A-Z][0-9]
false -> if anything in it other then those types or an empty string
Simple really...
Here's a simple addition to string functions (script access):
[ConsoleFunctions.cpp]
DefineConsoleFunction( isValidAlNum, bool, ( const char* str ),,
"Test whether the string contains ONLY Alpha-numeric characters (a-z, A-Z) or numbers (0-9).n"
"@param str The string to test.n"
"@return True if the string is an alpha-numeric string; false otherwise.nn"
"@ingroup Strings" )
{
int index = dStrlen( str );
bool result = false;
if( index <= 0 ) return false;
for( int i=0; i<index; i++)
{
result = dIsalnum( str[ i ] );
if( !result) return result;
}
return result;
}Just add it to the end of the file and recompile...
Usefull for checking Username and passwords. (I dont allow Sql injection).
There are probably simplier ways and can be done thru script, but C++ is faster.
Process:
call function with a string: %result = isValidAlNum( %username );
Result:
true -> if it contains [a-z][A-Z][0-9]
false -> if anything in it other then those types or an empty string
Simple really...
About the author