Well ... that gave me some work, but I think I finally got something quite interesting. Look:
With the existence of a point, if you try to add another one after this you will not be able to, so the point should be deleted. Instead of onKey
, I used onInput
, which will detect if something is changed, so that the user can "navigate" the lestras without being played to the end of input
, besides the performance question, because in that if it is imperceptible to delete the point.
$('input').on("input", function(){
var val = this.value;
var test = /^[^.]?.[^.]*$/;
if(test.test(val)){
return;
}else{
var n = val.search(/\./);
var beforeDot = val.substring(0, n);
var dot = val.substring(n, n+1);
var afterDot = val.substring(n+1, val.length);
$(this).val(beforeDot.replace(/\./g, "") + dot + afterDot.replace(/\./g, ""));
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputtype="text" />