I have a function in javascript where I only allow numbers, commas (,) and hyphens (-) in an input.
The problem is that I can type as many commas (,) and hyphens (-) as I want. I would like to know how I can only allow one comma or less.
My javascript
// Somente numeros e , e -
function SomenteNumero(e) {
var tecla = (window.event) ? event.keyCode : e.which;
if ((tecla > 47 && tecla < 58 || tecla === 44 || tecla === 45 || tecla === 13))
return true;
else {
if (tecla === 8 || tecla === 0)
return true;
else
return false;
}
}
I think so in the input:
<input type='text' name='mg' onkeypress='return SomenteNumero(event)'>