Lock of characters with exception e-mail field

Asked

Viewed 112 times

1

I’m using this code to block the special characters, but this blocking also in the email field. how do I resolve? or how do I allow @ and _

$(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, ''));
});
})
  • You just need to add inside the brackets:

2 answers

4


You just need to add them inside the brackets:

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

3

I don’t know if I understand 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 obsolete, 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>
	<input type="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>

Browser other questions tagged

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