Im trying to build a scoreboard where you cannot input a name if it has special characters or if it contains spaces.
Tried something like this
const name = nameInput.value;
const test = /[A-z 0-9 ^\s]/.test(name)
But it doesn't work.
Im trying to build a scoreboard where you cannot input a name if it has special characters or if it contains spaces.
Tried something like this
const name = nameInput.value;
const test = /[A-z 0-9 ^\s]/.test(name)
But it doesn't work.
^ to match start and $ to match end,
/^[a-z0-9]+$/i.test("test"); => true
/^[a-z0-9]+$/i.test("test9"); => true
/^[a-z0-9]+$/i.test("test9 "); => false
/^[a-z0-9]+$/i.test("testæøå"); => false
ofc if you need unicode support, it gets a whole lot harder, not sure how to solve that.. (sure you could change the regex to /^[a-z0-9æøå]+$/iu
to support Norwegian unicode characters, but that still leaves out Swedish unicode characters, you could add them but that would still leave out Chinese characters, and so on...)
If you're ok with with an underscore (_), this should also do:
/^\w+$/.test("abiAcd9")
\w
matches [A-Za-z0-9_]