-2

Looking to see how I can edit the Username creation process to allow underscores and hyphens at the beginning and end of usernames.

Currently, if you end your username with a _, it drops it from the creation process.

$regex = '/^[A-Za-z0-9]+[A-Za-z0-9_.]*[A-Za-z0-9]+$/';
if(!preg_match($regex, $_POST['username'])) {
        $_SESSION['error'][] = $language->register->error_message->username_characters;
    }
LoboStylez
  • 13
  • 3
  • You may replace `[A-Za-z0-9]` parts with `[A-Za-z0-9_]` or `\w`, but you may also try `'/^\w(?:[\w.]*\w)?$/'` – Wiktor Stribiżew May 06 '19 at 06:42
  • Thanks for the reply Wiktor! -- Would putting [A-Za-z0-9_] in the first and third positions work? I'm not entirely familiar with the actual order of operations / happenings in this line of code to be honest. -- So in turn, the first line should look like this? $regex = '/^[A-Za-z0-9_]+[A-Za-z0-9_.]*[A-Za-z0-9_]+$/'; – LoboStylez May 06 '19 at 06:50
  • The point is that your regex requires the string to have at least 2 chars. I suppose 1 char strings should also be valid, shouldn't they? – Wiktor Stribiżew May 06 '19 at 06:51

1 Answers1

0

You just need to add underscore _ and hyphen - to your first and last character set to allow your username to start or end with those two new characters and write your regex like this,

^[A-Za-z0-9_-]+[A-Za-z0-9_.]*[A-Za-z0-9_-]+$

and as \w is same as writing [a-zA-Z0-9_] hence you can compact your regex to this,

^[\w-]+[\w.]*[\w-]+$

Just want to also mention one point that whenever you write a hyphen - in a character set, make sure to always place it as either the first or last character in the character set, else unknowingly, the hyphen may act either as a range specifier and may not act as a literal hyphen. Although as in above regex, there is only \w and - in the character set, hence we don't need to worry here about the placement of hyphen.

Regex Demo

Also, I am not sure if you want to allow usernames (unlike a variable name which generally is allowed to be just one character) of just one character, but if you do, then you can modify your regex to this,

^[\w-]+([\w.]*[\w-]+)?$

Regex Demo allowing just one character as username

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • So final result should look like this?: $regex = '/^[\w-]+[\w.]*[\w-]+$/'; -- want to make sure if i need the slashes? – LoboStylez May 06 '19 at 06:53
  • @LoboStylez: Yes that's correct. And in PHP you write your regex in quotes and just after quote and before quote, you need to write a regex delimiter hence you need to write `/` too but if you want, you can change those delimiters to something else like `#` or `~` but you do need to write at least some delimiter. Generally you would like to choose a delimiter that doesn't occur in the regex else you need to escape it and hence to avoid uselessly escaping the char in regex, it is wise to choose a delimiter that doesn't appear in regex. – Pushpesh Kumar Rajwanshi May 06 '19 at 06:55