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?
– user28595
No, he has to block all the special characters, except Hifen
– Marcus Daniel
Your regex says otherwise, in it you are allowing numbers, letters, letters with accent. All these are considered alphanumeric.
– user28595
Yes, but I want to allow the hyphenate too.
– Marcus Daniel
^[ 0-9a-z\-A-Zàèìòùáéíóúâêîôûãõ\b]+$
see if it works.– user28595
It was good, yours too :) @Diegofelipe
– Marcus Daniel