How to do regular expression to accept words and then 2 numbers with jQuery, no special characters?

Asked

Viewed 931 times

2

Hello, everyone. Next, I am making a regular expression to accept the following: 02 word. Word and numbers, not just numbers. I’m using this code:

$(".inputNomeTurma").keyup(function() {
    var valor = $(this).val().replace(/(([a-zA-Z]*\d{3,})|[!"#$%&'()*+ºª,-./:;<=>?@[\]_{|}])/,'');
    $(this).val(valor);
});

It is ok, if I click a * character for example, it replaces it. But if I press and hold *, the field accepts the special character.

  • Being very simple add the character g at the end of its regex /REGEX/g, whether it works.

  • I didn’t quite understand but see if this is it https://regex101.com/r/sU8fF3/3

1 answer

1

From what I understand it must solve

/[^\w]/g

[^\w] corresponde a um caractere único não está presente na lista abaixo
   \w corresponde a qualquer caractere de palavra [a-zA-Z0-9_]
modificador g: global

or maybe this fits better in your problem:

$(".inputNomeTurma").keyup(function() {
    var valor = $(this).val();
    var reg   = /([a-zA-Z])+([0-9]{2})/g;
    var encontrados = reg.exec(valor);
    console.log(encontrados);
    $(this).val(encontrados[0]);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.