c++ – Simple username validation

Instead of the loop, we can use std::all_of() (or std::any_of() with the opposite condition).

And we have std::isalnum(), which is likely implemented as a lookup table like this (with the caveat that we need to be careful about signed char). It’s more portable, too – the code we have will be wrong if there are non-alphabetic characters between A and Z, as there are on EBCDIC systems, for example.

We could change the interface to just return the error string, with a null pointer indicating success, to avoid the “out” parameter.

#include <algorithm>
#include <cctype>
#include <string>
// return a null pointer if valid, else a pointer to the error message
const char *check_username_valid(const std::string& str)
    if (str.length() < MinUsernameLen) { return "Username Too Short"; }
    if (str.length() > MaxUsernameLen) { return "Username Too Long"; }
    auto const char_permitted
        = ()(unsigned char c){ return c == '_' || std::isalnum(c); };

    if (std::all_of(str.begin(), str.end(), char_permitted)) {
        return nullptr;

    return "Invalid Character in Username";

Minor: no need for cast to int when indexing (a(int(((unsigned char)(x))))). An unsigned char is perfectly fine as array index.