Greetings. I ask for help in making a regular account to verify the login. Login must contain from 4 to 10 characters and may consist of numbers and letters of the Latin alphabet and contain an underscore and / or hyphen.

Here is the mock code, but, you guessed it, it doesn't work.

if(/[^0-9a-zA-Z\-\_]{4,10}$/g.test(str)) res.innerHTML = 'good'; else res.innerHTML = 'bad'; 
  • one
    ^ should be in front of the square brackets, otherwise xs, but restrict users with the same framework - the last century - Specter
  • 2
    > / ^ [\ w _] {4,10} $ / ig So it should work out. - ReinRaus
  • Hmm .. what do you suggest then? Allow any characters? - LightShock
  • one
    @ReinRaus, _ is included in \\ w, but not -, therefore: /^[\w-]{4,10}$/i - Alex Kapustin
  • 2
    Standard practice is a login from Latin and numbers and a nickname from any characters. To eliminate visual clones of nicks, you can write another regular expression. - ReinRaus

1 answer 1

In principle, as written by ReinRaus

  if(/^[\w-]{4,10}$/i.test(str)) res.innerHTML = 'good'; else res.innerHTML = 'bad';