4
The problem I’m facing is that I can’t let the user type more than one comma into input
(I am using ASP.NET with Behind in C#), but also I cannot use the input type="number"
because the version of the framework I am using does not let.
I tried some things with Javascript but did not succeed. Follow the code of input
:
function SomenteNumero(e) {
var tecla = (window.event) ? event.keyCode : e.which;
if ((tecla > 47 && tecla < 58)) return true;
else {
if (tecla == 8 || tecla == 0 || tecla == 44 || tecla == 13) return true;
else return false;
}
}
var input = document.getElementById('NR_PESO');
var oldVal = '';
input.addEventListener('keyup', function () {
var parts = this.value.split(',');
if (parts && parts[1] && parts[1].length > 3) this.value = oldVal;
oldVal = this.value;
});
<input type="text" class="form-control" runat="server" maxlength="11" ID="NR_PESO" onkeypress='return SomenteNumero(event)' placeholder="XXXXXXX,XXX">
I am saving this information in the Mysql database as decimal(10,3)
so I need only three houses after the comma and only one comma (always has the client smart enough to put a '123,3,43' or '123,343'. I accept jQuery too.