update restrict possible usernames solution (#35973)

This commit is contained in:
Tom
2019-05-05 15:37:03 -05:00
committed by Randell Dawson
parent a24ecfbd85
commit 3cecf26f44

View File

@ -6,21 +6,15 @@ title: Restrict Possible Usernames
## Solution: ## Solution:
```javascript ```javascript
let username = "JackOfAllTrades"; let username = "JackOfAllTrades";
let userCheck = /^[a-z]{2,}\d*$/i; const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
let result = userCheck.test(username); let result = userCheck.test(username);
``` ```
## Explain: ## Explain:
1. The only numbers in the username have to be at the end. `\d$` 1. `^` - start of input
There can be zero or more of them at the end. `*` 2. `[a-z]` - first character is a letter
```javascript 3. `[0-9]{2,0}` - ends with two or more numbers
/\d*$/; 4. `|` - or
``` 5. `[a-z]+` - has one or more letters next
2. Username letters can be lowercase and uppercase. `i` 6. `\d*` - and ends with zero or more numbers
```javascript 7. `$` - end of input
/\d*$/i; 8. `i` - ignore case of input
```
3. Usernames have to be at least two characters long. `{2,}`
A two-letter username can only use alphabet letter characters. `^[a-z]`
```javascript
/^[a-z]{2,}\d*$/i;
```