Lock special characters but allow hyphen

Asked

Viewed 7,764 times

0

My code for locking characters is like this:

 $('#nome').on('keypress', function() {
      var regex = new RegExp("^[ 0-9a-zA-Zàèìòùáéíóúâêîôûãõ\b]+$");
      var _this = this;
      // Curta pausa para esperar colar para completar
      setTimeout( function(){
          var texto = $(_this).val();
          if(!regex.test(texto))
          {
              $(_this).val(texto.substring(0, (texto.length-1)))
          }
      }, 100);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='nome'/>

I want to know if there is any way to block the special characters, but allowing the use of hyphen (-)

  • You want to actually allow only alphanumeric and hyphenate, correct?

  • No, he has to block all the special characters, except Hifen

  • Your regex says otherwise, in it you are allowing numbers, letters, letters with accent. All these are considered alphanumeric.

  • Yes, but I want to allow the hyphenate too.

  • 1

    ^[ 0-9a-z\-A-Zàèìòùáéíóúâêîôûãõ\b]+$ see if it works.

  • It was good, yours too :) @Diegofelipe

Show 1 more comment

2 answers

3


Just you make a small change in your regex allowing this. Your regex would look like this:

RegExp("^[a-zA-Z0-9-Zàèìòùáéíóúâêîôûãõ\b]+$");

example would look like this:

 $('#nome').on('keypress', function() {
      var regex = new RegExp("^[a-zA-Z0-9-Zàèìòùáéíóúâêîôûãõ\b]+$");
      var _this = this;
      // Curta pausa para esperar colar para completar
      setTimeout( function(){
          var texto = $(_this).val();
          if(!regex.test(texto))
          {
              $(_this).val(texto.substring(0, (texto.length-1)))
          }
      }, 100);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="nome"/>

  • 1

    Without escaping the hyphenate?

  • @Diegofelipe What do you mean? His question is to exactly leave only the hyphen.

  • Escape the hyphen in the regex, like this: ^[ 0-9a-z\-A-Zàèìòùáéíóúâêîôûãõ\b]+$

  • 1

    Problem solved, Thanks ai @Randrade

3

add the hyphen as the last class character, so it is not necessary to escape it:

new RegExp("^[ 0-9a-zA-Zàèìòùáéíóúâêîôûãõ\b-]+$");

Your code only removes the last character from the field, this will be a problem if you paste text with invalid characters. The ideal is to remove all invalid characters with a denied class, which starts with a circumflex (e. g. [^a-z]).

Another improvement I suggest is to use the event input that works better than keypress for pasted content.

$(function(){
  var regex = new RegExp('[^ 0-9a-zA-Zàèìòùáéíóúâêîôûãõ\b-]', 'g');
  // repare a flag "g" de global, para substituir todas as ocorrências
  $('input').bind('input', function(){
    $(this).val($(this).val().replace(regex, ''));
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text"/>

Browser other questions tagged

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