Community
|
March 14, 2022
Username-with-only-letters-numbers-and-underscore
Community

Program To Verify Username With Only Letters, Numbers And Underscore

This article will provide you with a way to check if a string contains alphanumeric characters with underscores. In other words, determine whether a string consists of only letters, numbers, underscore, and not any other character.

Regular Expressions are the best way to identify patterns within strings. They seem to be a bit difficult and irritating, but once you know how to use them, they're amazing!.

Basically, a regular expression is a pattern describing a certain amount of text. For example, we know that emails are always like:

username@domain.extension

If we want to describe the pattern of an email, we will say something like this: Starting with a username (a combination of letters and numbers), followed by an @ symbol, followed by the domain (another combination of letters and numbers) followed by the extension (that starts with a dot . followed by a combination of only letters).

The process of describing the pattern of an email is the same process you will follow when you want to create a regular expression. The only difference will be the syntax:

 

The regex you are looking for is:

[A-z_]

The length restriction for example can be edited like this:

{4,15}

So you put in your /conf/config.php file:

$Configuration['Garden']['User']['ValidationRegex'] = '[A-z_]';

$Configuration['Garden']['User']['ValidationLength'] = '{4,15}';

Probably change the translation here too:

$Definition['UsernameError'] = "Username can only contain letters, numbers, and underscores, and must be between 4 and 15 characters long.";