1
I’m writing a Function to validate only numbers in one case and only letters in another, however, when I only had a Function to validate Noughts, Function worked, when I have both at the same time, both go wrong.
function somenteNumeros(num) {
var er = /[^0-9]/;
er.lastIndex = 0;
var campo = num;
if (er.test(campo.value)) {
campo.value = "";
}
}
function somenteLetras(letra) {
var er = /[^a-zA-Z]/s;
er.lastIndex = 0;
var campo = letra;
if(er.test(campo.value)){
campo.value = "";
}
}
<input type="text" size="35" name="nomeOutro" onkeyup="somenteLetras(this)" minlength="3" maxlength="150"/>
<input type="text" size="1" name="idade" onkeyup="somenteNumeros(this)" maxlength="3"/>
How can I leave allowed space then ?
– Caio Vieira
@Caiovieira I added this possibility in the answer.
– Sergio
It worked, thank you
– Caio Vieira