Character block with exception field email

1

I am using this code to block special characters, but it is also blocking in the email field. How do I solve it? or as I allow. " e _

$(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, ''));
});
})
    
asked by anonymous 13.09.2017 / 14:23

2 answers

4

You just need to add them inside the square brackets:

var regex = new RegExp('[^ 0-9a-zA-Zàèìòùáéíóúâêîôûãõ\b-@_]', 'g');
    
13.09.2017 / 14:26
3

I do not know if I understood your question correctly, but in your code you apply the expression to all inputs.

An alternative is to define the inputs you want to validate through a class.

Note: the .bind () command is deprecated, use .on ()

  

As of jQuery 3.0, .bind () has been deprecated. It was superseded by   the .on () method for attaching event handlers

Example:

$(function(){
   var regex = new RegExp('[^ 0-9a-zA-Zàèìòùáéíóúâêîôûãõ\b-]', 'g');
   $(".validar").on('input', function(){
      $(this).val($(this).val().replace(regex, ''));
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div><inputtype="text" name="nome" id="nome" class="validar"    placeholder="Digite seu nome" />
</div>
<div>
	<input type="text" name="idade" id="idade" class="validar" placeholder="Digite sua idade" />
</div>
<div>
	<input type="text" name="email" id="email" placeholder="Digite seu e-mail"/>
</div>
    
13.09.2017 / 14:30