3
Guys, I have a javascript function where I allow only numbers, comma (,) and hyphen (-) in an input.
The problem is that I can type as many commas (,) and hyphens (-) as I want. I wanted to know how to allow just 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 find it so in input:
<input type='text' name='mg' onkeypress='return SomenteNumero(event)'>
What pattern are you looking for? Currency? Two numbers separated by a comma? A number that allows fractions? Please explain in more detail.
– user21448
Will the hyphen be used to identify negative numbers or can it exist in the middle of the number? You want to allow numbers like
-10
,10
,10,11
,-10,11
,0
,-0
,0,123
and-0,123
?– user21448
the hyphen is only for negative numbers, that is, it can only be used at the beginning
– Hugo Borges