1

I'm using a regular expression in javascript that allows only numbers and two kinds of separators "," or "." This regular expression is the following:

/^[0-9]+([,.][0-9]+)?$/g

Now, it's mandatory that if a user tries to enter a character or something else besides a number with , or . separator to prevent him. To be more specific if a user types 123asv I should display only the 123.

So i wrote this:

value.replace(/[0-9]+([,.][0-9]+)?$/g, '');  

But this is not allowing enter number. This is reasonable because i replace every value that matches the above regex with space. How to edit it so as to achieve the desired functionality?

Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
RamAlx
  • 6,976
  • 23
  • 58
  • 106
  • Is `1,sf4` an expected input with `1,4` as expected output? Can letters or just any non-digit symbols appear anywhere? – Wiktor Stribiżew Aug 11 '17 at 07:26
  • Yes, the output should be 13,4 or 13.4 or 13. Every other character should be prevented – RamAlx Aug 11 '17 at 07:27
  • 1
    Try to use `value = value.replace(/[^\d,.]+|[.,](?=.*[,.])/g, '')`, and then `value.test(/^[0-9]+([,.][0-9]+)?$/)`. – Wiktor Stribiżew Aug 11 '17 at 07:30
  • That's fine but i see a weird behaviour. I have a function to preventr more than three decimal poin { let decimalIndex; if (e.target.value.includes('.')) { decimalIndex = e.target.value.indexOf('.'); } else { decimalIndex = e.target.value.indexOf(','); } console.log(decimalIndex); if (decimalIndex > -1) { const decimals = e.target.value.substring(decimalIndex, e.target.value.length); if (decimals.length > 3 && e.keyCode !== 8) { e.preventDefault(); } } }; By applying the value.replace with the above regex the onKeyDown is not applied – RamAlx Aug 11 '17 at 07:38
  • 1
    If you do not care about the commas and periods, just use `value = value.replace(/[^\d,.]+/g, '')` – Wiktor Stribiżew Aug 11 '17 at 07:40
  • Make it an aswer :) Thanks a lot :) – RamAlx Aug 11 '17 at 07:43

1 Answers1

1

In your current scenario, you need to remove any digits, , and . symbols from the input string, since you have extra code that validate/sanitizes extra comma/periods:

value = value.replace(/[^\d,.]+/g, '')

The [^\d,.]+ pattern matches 1 or more chars that are not digits, . and ,, and g modifier matches multiple occurrences. See this regex demo.

Note that in case there is no additional code to validate separators, you may add an alternative to remove all . and , but their last occurrences:

value = value.replace(/[^\d,.]+|[.,](?=.*[,.])/g, '')
                               ^^^^^^^^^^^^^^^ 

See this regex demo. The [.,](?=.*[,.]) matches a , or . that are followed with any 0+ chars other than line break chars and then another , or ., but only the first matched . or , are removed since the (?=...) lookahead is a non-consuming construct.

Then test if the value matches the number pattern:

if (value.test(/^[0-9]+([,.][0-9]+)?$/)) { ... }

Do not use the global modifier with the RegExp#test() method.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563