0

I am working on a textfield to make it allow only numbers with max one comma and one dot(max) at any occurrence. It can accept value like "9.8,8.6". It should not allow two or more dots or commas at a time.

I have tried below code but its not working.

this.regex = new RegExp('[\d,]\.?[\d,]*$');
Sunil Bamal
  • 87
  • 1
  • 14

1 Answers1

1

Your current pattern has a lot of optional parts. It would also allow for example a single comma.

If the max is only 1 comma and 1 dot per number before or after the comma, you could make it optional:

^(?:\d+(?:\.\d+)?|\.\d+)(?:,(?:\d+(?:\.\d+)?|\.\d+))*$
  • ^ Start of string
  • \d+ Match 1+ digits
  • (?:\.\d+)* Match 0+ times a dot and 1+ digits
  • (?: Non capturing group
    • ,\d+(?:\.\d+) match comma, 1+ digits and optionally a dot and 1+ digits
  • )? Close group and make it optional
  • $ End of string

Regex demo

Your code could look like (Note to double escape the backslashes)

this.regex = new RegExp('^(?:\\d+(?:\\.\\d+)?|\\.\\d+)(?:,(?:\\d+(?:\\.\\d+)?|\\.\\d+))*$');

let regex = new RegExp('^(?:\\d+(?:\\.\\d+)?|\\.\\d+)(?:,(?:\\d+(?:\\.\\d+)?|\\.\\d+))*$');

[
  "9.8,8.6",
  "9.8",
  "8.2,9",
  "5,9",
  "9.8,8.6,1.1",
  "1,8.6",
  "9.8,8.6,1.1",
  "8,8,8",
  "9.8,8,8",
  "1,1",
  "1,1,.1,1",
].forEach(s => {
  console.log(s + " ==> " + regex.test(s));
});
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • thanks for the quick reply, do I need to use it like this "this.regex = new RegExp('^\d+(?:\.\d+)(?:,\d+(?:\.\d+))?$');" – Sunil Bamal Aug 05 '19 at 13:58
  • @SunilBamal I have added an example of how you might use it. You have to double escape the backslashes as well. – The fourth bird Aug 05 '19 at 14:07
  • @SunilBamal I have updated the example, it does when you use a `*` instead of a `?` – The fourth bird Aug 05 '19 at 14:11
  • thanks @The, it fails for value "8.2,9" but working perfect for all decimal values, it can be number without decimal as well, pls suggest, even fails for "5,9" – Sunil Bamal Aug 05 '19 at 14:22
  • @SunilBamal No your question is not confusing, I forgot to make the last group optional. I have updated the answer. – The fourth bird Aug 05 '19 at 14:30
  • thanks alot @The, you saved my day.. just one change required if possible its not taking first value as dot but a value can be something like ".5,9", but i really appreciate the way you help me out, thanks alot.. – Sunil Bamal Aug 05 '19 at 14:38
  • @SunilBamal Do you mean like this? `^\d*(?:\.\d+)?(?:,\d*(?:\.\d+)?)*$` https://regex101.com/r/pYy4Hm/1 – The fourth bird Aug 05 '19 at 14:43
  • please help for this link https://stackoverflow.com/questions/57374841/how-implement-regex-for-a-textfield-to-allow-one-comma-one-space-at-a-time-in-ex – Sunil Bamal Aug 06 '19 at 11:21
  • @SunilBamal You could add an optional space or match 0+ times using `*` see `let regex = new RegExp('^(?:\\d+(?:\\.\\d+)?|\\.\\d+)(?: ?, ?(?:\\d+(?:\\.\\d+)?|\\.\\d+))*$');` and https://rextester.com/EWLY40601 – The fourth bird Aug 06 '19 at 11:24
  • dot is not required this time @The, also please answer there so that I can accept it .. – Sunil Bamal Aug 06 '19 at 11:27
  • 1
    thanks @The this.regex = regex = new RegExp('^(?:\\d+(?:\\d+)?|\\d+)(?: ?, ?(?:\\d+(?:\\d+)?|\\d+))*$'); this is working for me you are great.. – Sunil Bamal Aug 06 '19 at 11:41