javascript – Regular expression that does not allow spaces at the end


I am working with this expression /^([a-zA-Z0-9]){1,16}$/ which validates a field that can only enter alphanumerics with a maximum length of 16 characters, but I need to add that it does not allow spaces at the end.

I've tried adding [:space:] or \s to the end and it doesn't work for me.

Who can guide me?

Kind regards


As it is proposed, it does not allow spaces, neither before nor between nor after. Use the following template to test regexp

document.getElementById('dale').onclick = (ev) => {
  let exp = document.getElementById('laExpresion').value;
  let re = new RegExp(exp);
  console.log("/" + exp + "/", re.test(document.getElementById('elTexto').value));
Expresión: <input type="text" id="laExpresion" value="^([a-zA-Z0-9]){1,16}$" /><br />
Texto: <input type="text" id="elTexto" />
<button id="dale">validar</button>
Scroll to Top