Home > other >  Simple username RegEx for JavaScript
Simple username RegEx for JavaScript

Time:11-15

I have a username field that can consist of A-Za-z0-9_ and must be between 3 and 20 characters.

In python, it's simply: ^[A-Za-z0-9_]{3,20}\Z

But I can't seem to get it to work in JavaScript. I've tried:

/^[A-Za-z0-9_]{3,20}\\/
/^[A-Za-z0-9_]{3,20}\\Z/
/^[A-Za-z0-9_]{3,20} $/
/^[A-Za-z0-9_]{3,20}/

What am I missing here? Thank you.

CodePudding user response:

Use: /^[A-Za-z0-9_]{3,20}$/g

console.log(/^[A-Za-z0-9_]{3,20}$/g.test("spectric"))
console.log(/^[A-Za-z0-9_]{3,20}$/g.test("spectric#"))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related