2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Restrict Possible Usernames
|
|
|
|
---
|
|
|
|
## Restrict Possible Usernames
|
|
|
|
|
|
|
|
## Solution:
|
|
|
|
```javascript
|
|
|
|
let username = "JackOfAllTrades";
|
2019-05-05 15:37:03 -05:00
|
|
|
const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
|
2018-10-12 15:37:13 -04:00
|
|
|
let result = userCheck.test(username);
|
|
|
|
```
|
|
|
|
## Explain:
|
2019-05-05 15:37:03 -05:00
|
|
|
1. `^` - start of input
|
|
|
|
2. `[a-z]` - first character is a letter
|
|
|
|
3. `[0-9]{2,0}` - ends with two or more numbers
|
|
|
|
4. `|` - or
|
|
|
|
5. `[a-z]+` - has one or more letters next
|
|
|
|
6. `\d*` - and ends with zero or more numbers
|
|
|
|
7. `$` - end of input
|
|
|
|
8. `i` - ignore case of input
|