Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
regex match () no funciona con input.value

Tengo un problema con las expresiones regulares.

En mi código tengo una entrada cuando un jugador puede escribir su nombre y luego hacer clic en el botón 'start' para comenzar la aventura.

La primera condición está funcionando bien. El jugador recibe un error cuando el nombre no tiene entre 4 y 20 caracteres.

Cuando se trata de la siguiente condición. El jugador obtiene un error sin importar lo que escriba dentro de la entrada. Cuando cambié match() a test() hay un error en la consola, que test() no es una función.

Por favor ayúdenme, ¿qué hice mal?

 let actualPlace = 'mainMenu'; let playerName = ''; const playerNameContainer = document.querySelector('.playerName'); const start = document.querySelector('.start'); const regExpression = /^[a-zA-ZżźćńółęąśŻŹĆĄŚĘŁÓŃ0-9]$/; start.addEventListener('click', function(){ if (actualPlace == 'mainMenu') { playerNameValue = playerNameContainer.value; playerNameLength = playerNameValue.length; if (playerNameLength >= 4 && playerNameLength <= 20) { if (playerNameValue.match(regExpression)) { actualPlace = 'adventure'; playerName = playerNameContainer.value; playerNameContainer.value = ''; } else { errorMsg.innerHTML = 'error'; } } else { errorMsg.innerHTML = 'error'; } } })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

su expresión regular coincide con un solo carácter; necesita agregar el cuantificador + para que coincida con más de un carácter.

También agregué diferentes errores para que sepas qué parte está fallando.

Aquí es un ejemplo de trabajo:

 let actualPlace = 'mainMenu'; let playerName = ''; const playerNameContainer = document.querySelector('.playerName'); const start = document.querySelector('.start'); const regExpression = /^[a-zA-ZżźćńółęąśŻŹĆĄŚĘŁÓŃ0-9]+$/; const errorMsg = document.querySelector('.error'); start.addEventListener('click', function(){ if (actualPlace == 'mainMenu') { playerNameValue = playerNameContainer.value; playerNameLength = playerNameValue.length; if (playerNameLength >= 4 && playerNameLength <= 20) { if (playerNameValue.match(regExpression)) { actualPlace = 'adventure'; playerName = playerNameContainer.value; playerNameContainer.value = ''; errorMsg.innerHTML = 'Success!'; } else { errorMsg.innerHTML = 'error: your name must not contain spetial characters'; } } else { errorMsg.innerHTML = 'error: your name must be between 4 and 20 characters'; } } })
 <input class="playerName" /><br /> <button class="start" >Start</button> <div class="error" ></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!