2

I am trying to limit an input to a decimal number. I'd like any invalid characters not to be displayed at all (not displayed and then removed). I already have this implemented but for whole integers (like input for a phone number) but now I need to apply it for decimal input.

Sample input/output:

default value 25.00  -> type 2b5.00  -> display 25.00
default value 265.50 -> type 2.65.50 -> display 265.50 (as if prevented decimal point from being entered)
default value 265.52 -> type 265.52. -> display 265.52 (same as previous one)

End New Edit

I found many threads that dealt with "decimal input" issue but almost 99% of them deal only with "match"ing and "test"ing the input while my need is to replace the invalid characters.

Other than trying many regexes like /^\d+(\.\d{0,2})?$/, I also tried something like the below which keeps only the first occurrence in the input. This still isn't my requirement because I need the "original" decimal point to remain not the first one in the new input. This was the code for it:

[this.value.slice(0, decimalpoint), '.', this.value.slice(decimalpoint)].join('')

This thread is the closest to what I need but since there was no response to the last comment about preventing multiple decimal points (which is my requirement), it wasn't useful.

Any help would be appreciated.

Community
  • 1
  • 1
RMK
  • 456
  • 2
  • 9
  • 19

1 Answers1

2

Outline: find the first ., split there and clean the parts, else just return cleaned value.

function clean(string) {
    return string.replace(/[^0-9]/g, "");
}
var value = "a10.10.0";
var pos = value.indexOf(".");
var result;
if (pos !== -1) {
    var part1 = value.substr(0, pos);
    var part2 = value.substr(pos + 1);
    result = clean(part1) + "." + clean(part2);
} else {
    result = clean(value);
}
console.log(result); // "10.100"
Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • it fails for a value like this var value = "aasdf.a10.10.0"; – Rainer Plumer Oct 31 '13 at 15:17
  • It doesn't fail. It gives `.10100`. Which is expected. What do you think it should do? – Halcyon Oct 31 '13 at 15:20
  • To be honest, im not sure. I thought it should start from the first number, and give 10.10.0 – Rainer Plumer Oct 31 '13 at 15:23
  • Thanks for the reply. I tried this and it works for the most part, I don't mind it starting with a dot, I can pad as needed. One question, can this be modified to work with this example? `default value 265.50 -> type 2.65.50 -> display 265.50` Because as it stands, it gives `2.6550`. So I want it to be as if the new decimal point wasn't accepted in the first place. – RMK Nov 01 '13 at 10:50
  • If you want to use the _last_ `.` instead: change `indexOf` to `lastIndexOf`. `a10.10.0` will give `1010.0` then. – Halcyon Nov 01 '13 at 12:07
  • But then this `default value 265.52 -> type 265.52. -> display 265.52` 'fails' and gives `26552.`; same for `default value 265.52 -> type 265.5.2 -> display 265.52` which gives `2655.2` I know this might need a bit more logic but I can't seem to wrap my head around how it can be done. – RMK Nov 01 '13 at 12:44
  • Your logic seems ambiguous. How do you decide which `.` to keep? – Halcyon Nov 01 '13 at 12:48
  • Basically the one that was already in the amount before the key was pressed. I think this requires either the oldvalue to be stored somehow or catching the character that was input which can get a little messy.. Unless there's a more efficient way of doing it. – RMK Nov 01 '13 at 14:26