Javascript regex for base digits with decimal points?

I am trying to use regex to test decimal values. I wrote a regex below, but does not allow the first decimal place with a value like .5 or .6 or .1

Regular Exp : /^\d[0-9]{0,13}(\.\d{1,2})?$/

Rules:

  • He must admit positive numbers.
  • It must allow a maximum of 13 numbers to a decimal point
  • This should allow max two numbers after the decimal number.
  • He must allow . (dot) with a number like .5
  • Cannot be allowed .0

Example - Valid Inputs

  • 0
  • 0.5
  • 1.55
  • 0.5
  • 1234567890123 (13 numbers to decimal places)
  • +1234567890123.5
  • +1234567890123,00

Example - Invalid Inputs

  • . (point),
  • .0
  • 1,234
  • 5.
  • 12345678901234 (14 numbers to decimal places)
  • +12345678901234.56

 const valid = [ "0", "0.5", "1.55", ".5", "1234567890123", "1234567890123.5", "1234567890123.00", ]; const invalid = [ ".", ".0", "1.234", "5.", "12345678901234", "12345678901234.56", ]; const rgx = /^\d[0-9]{0,13}(\.\d{1,2})?$/ console.log("Checking valid strings (should all be true):"); valid.forEach(str => console.log(rgx.test(str), str)); console.log("\nChecking invalid strings (should all be false):"); invalid.forEach(str => console.log(rgx.test(str), str)); 
+5
source share
4 answers

I believe the following regular expression should meet all your criteria:

 ^(\d{1,13}($|\.\d?\d$)|\.[1-9]\d?$) 

first case: 1-13 digits followed by either nothing or ".". followed by one or two digits

second case: a "." followed by a non-zero digit and no more than one other digit

 const valid = [ "0", "0.5", "1.55", ".5", "1234567890123", "1234567890123.5", "1234567890123.00", ]; const invalid = [ ".", ".0", "1.234", "5.", "12345678901234", "12345678901234.56", ]; const rgx = /^(\d{1,13}($|\.\d?\d$)|\.[1-9]\d?$)/ console.log("Checking valid strings (should all be true):"); valid.forEach(str => console.log(rgx.test(str), str)); console.log("\nChecking invalid strings (should all be false):"); invalid.forEach(str => console.log(rgx.test(str), str)); 
+4
source

I think this should fulfill most of your requirement, but not all of them, limit to nine decimal places

 ( /^(\d+\.?\d{0,9}|\.\d{1,9})$/ ) 

and this one without a decimal limit

 ( /^(\d+\.?\d*|\.\d+)$/ ) 
+1
source

Other answers do not allow the use of the .0x case, which I believe is valid? I think you need to check for xxx [.xx] and .xx separately ie

 ^(\d{1,13}(\.\d{1,2})?|\.(0[1-9]|[1-9]\d?))$ 
+1
source

To match your values, you can use a non-exciting group with alternation with | and indicate what you want to match.

^(?:\.[1-9][0-9]?|\d{1,13}(?:\.\d{1,2})?|.\d{2})$

This will also match .01 , not .0

Explanation

  • ^ Start line
  • (?: Not an exciting group
    • \.[1-9][0-9]? Matches a point not followed by 0 and then 0-9
    • | Or
    • \d{1,13} Match 1 - 13 digits
    • (?: Not an exciting group
      • \.\d{1,2} Match the dot and 1 or 2 digits
    • )? Close the group without capturing and make it optional
    • | Or
    • .\d{2} Match dot followed by 2 digits
  • ) Close the group without capture
  • $ End of line

 const valid = [ "0", ".01", "0.5", "1.55", ".5", "1234567890123", "1234567890123.5", "1234567890123.00", ]; const invalid = [ ".", ".0", "1.234", "5.", "12345678901234", "12345678901234.56", ]; const rgx = /^(?:\.[1-9][0-9]?|\d{1,13}(?:\.\d{1,2})?|.\d{2})$/; console.log("Checking valid strings (should all be true):"); valid.forEach(str => console.log(rgx.test(str), str)); console.log("\nChecking invalid strings (should all be false):"); invalid.forEach(str => console.log(rgx.test(str), str)); 

To not match .01 , you can use:

^(?:\d{1,13}(?:\.\d{1,2})?|\.[1-9][0-9]?)$

 const valid = [ "0", "0.5", "1.55", ".5", "1234567890123", "1234567890123.5", "1234567890123.00", ]; const invalid = [ ".", ".0", ".01", "1.234", "5.", "12345678901234", "12345678901234.56", ]; const rgx = /^(?:\d{1,13}(?:\.\d{1,2})?|\.[1-9][0-9]?)$/; console.log("Checking valid strings (should all be true):"); valid.forEach(str => console.log(rgx.test(str), str)); console.log("\nChecking invalid strings (should all be false):"); invalid.forEach(str => console.log(rgx.test(str), str)); 
+1
source

Source: https://habr.com/ru/post/1276052/


All Articles