Well... it gave me a certain job, but I think I finally got something quite interesting. Look:
With the existence of a point, if you try to add another after this you will not be able, for that the point should be deleted. Instead of the onKey
, I used the onInput
, which will detect if something is changed, with this the user can "browse" enters the reading without being played to the end of the input
, beyond the question of performance, in which case the exclusion of the.
$('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>
<input type="text" />
Remember to adjust the cursor position when filtering this type of input that changes the string size, otherwise it looks terrible to use the input.
– Bacco