how to ban other language characters in a username?

A

Alien

Say, I want to prevent user from inputting Russian character in
username, how to deal with it?
The codes I'm using now is:
------------------------------------------
for ( std::wstring::const_iterator it = userName.begin(); it !=
userName.end(); ++it)
{
// verify that each subsequent character is an alphanum
if (!std::isalnum(*it, std::locale::classic()) &&
// or is included in our valid chars set
(validCharsSet.find(*it) == validCharsSet.end()))
{
return false;
}
}
 
C

Chandu

You have to use imbue method on locales and bind facets to enable it
Check the classic textbook on C++ IOStreams and Locaes by Angelika
Langer and Kalus Kreft
 
F

Frederick Gotham

Alien posted:
Say, I want to prevent user from inputting Russian character in
username, how to deal with it?


int CharIsValid( char const c )
{
const char *p = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

do if( c == *p ) return 0; while ( *++p );

return 1;
}

int StringIsValid( const char *p )
{
while (*p) if ( !CharIsValid(*p++) ) return 0;

return 1;
}
 
F

Frederick Gotham

Frederick Gotham posted:

int CharIsValid( char const c )

int StringIsValid( const char *p )


I'd actually use a return type of "bool" in C++:


bool CharIsValid( char );

bool StringIsValid( const char * )
 
F

Frederick Gotham

Frederick Gotham posted:

bool CharIsValid( char );



I should really test my algorithms before posting! The one I posted was
garbage... I'll give it another go:


bool CharIsValid( char const c )
{
const char *p = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

do if( c == *p ) return true; while ( *++p );

return false;
}
 
J

Jakob Bieling

Frederick Gotham said:
Frederick Gotham posted:





I should really test my algorithms before posting! The one I posted
was garbage... I'll give it another go:

If you take this path, you might as well use code that has been
already written:
bool CharIsValid( char const c )
{
return strchr ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz",
c) != 0;
const char *p = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

do if( c == *p ) return true; while ( *++p );

return false;
}

regards
 
J

Jakob Bieling

Alien said:
Say, I want to prevent user from inputting Russian character in
username, how to deal with it?
The codes I'm using now is:
------------------------------------------
for ( std::wstring::const_iterator it = userName.begin(); it !=
userName.end(); ++it)
{
// verify that each subsequent character is an alphanum
if (!std::isalnum(*it, std::locale::classic()) &&
// or is included in our valid chars set
(validCharsSet.find(*it) == validCharsSet.end()))
{
return false;
}
}

I agree with Fredericks approach: create a string of *all* valid
chars and check if each entered character can be found in there. You
have done that already for a subset of the valid characters.

regards
 
A

Alien

Jakob said:
I agree with Fredericks approach: create a string of *all* valid
chars and check if each entered character can be found in there. You
have done that already for a subset of the valid characters.

regards

Can I set the local parameter to something like std::locale loc("C"),
so that still use the std::isalnum(...)?
template<Class CharType>
bool isalnum(
CharType _Ch,
const locale& _Loc
) const;

Just set the _Loc to pure English ASCII.
I've tried std::locale::classic() but failed.
 
A

Alien

Alien said:
Can I set the local parameter to something like std::locale loc("C"),
so that still use the std::isalnum(...)?
template<Class CharType>
bool isalnum(
CharType _Ch,
const locale& _Loc
) const;

Just set the _Loc to pure English ASCII.
I've tried std::locale::classic() but failed.

Note: The character is w_char
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top