By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 954 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

how to ban other language characters in a username?

P: n/a
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;
}
}
------------------------------------------
But it doesn't work.

Jun 29 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

Jun 29 '06 #2

P: n/a
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;
}
--

Frederick Gotham
Jun 29 '06 #3

P: n/a
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 * )

--

Frederick Gotham
Jun 29 '06 #4

P: n/a
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;
}
--

Frederick Gotham
Jun 29 '06 #5

P: n/a
Frederick Gotham <fg*******@SPAM.com> wrote:
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:


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
--
jb

(reply address in rot13, unscramble first)
Jun 29 '06 #6

P: n/a
Alien <Ma*****@gmail.com> wrote:
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;
}
}
------------------------------------------
But it doesn't work.


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
--
jb

(reply address in rot13, unscramble first)
Jun 29 '06 #7

P: n/a

Jakob Bieling wrote:
Alien <Ma*****@gmail.comwrote:
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;
}
}
------------------------------------------
But it doesn't work.

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
--
jb

(reply address in rot13, unscramble first)
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.

Jul 5 '06 #8

P: n/a

Alien wrote:
Jakob Bieling wrote:
Alien <Ma*****@gmail.comwrote:
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;
}
}
------------------------------------------
But it doesn't work.
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
--
jb

(reply address in rot13, unscramble first)

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

Jul 5 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.