Regex in JavaScript for decimal checking

I want a regular expression in JavaScript to test decimal numbers.

It must only allow up to two decimal places. For example, it should allow 10.89 , but not 10.899 .

It should also allow only one period ( . ). For example, it should allow 10.89 , but not 10.8.9 .

+13
javascript regex
Apr 05 2018-12-12T00
source share
12 answers

Try the following expression: ^\d+\.\d{0,2}$ If you want decimal places to be optional, you can use the following: ^\d+(\.\d{1,2})?$

EDIT: To test string matching in Javascript, use the following snippet:

 var regexp = /^\d+\.\d{0,2}$/; // returns true regexp.test('10.5') 
+31
Apr 05 2018-12-12T00:
source share
 ^\d+(\.\d{1,2})?$ 

will allow:

  • 244
  • 10.89
  • 9.5

prohibit:

  • 10.895
  • 10.
  • 10.8.9
+27
Apr 05 2018-12-12T00:
source share

Regex

 /^\d+(\.\d{1,2})?$/ 



demonstration

 var regexp = /^\d+(\.\d{1,2})?$/; console.log("'.74' returns " + regexp.test('.74')); console.log("'7' returns " + regexp.test('7')); console.log("'10.5' returns " + regexp.test('10.5')); console.log("'115.25' returns " + regexp.test('115.25')); console.log("'1535.803' returns " + regexp.test('1535.803')); console.log("'153.14.5' returns " + regexp.test('153.14.5')); console.log("'415351108140' returns " + regexp.test('415351108140')); console.log("'415351108140.5' returns " + regexp.test('415351108140.5')); console.log("'415351108140.55' returns " + regexp.test('415351108140.55')); console.log("'415351108140.556' returns " + regexp.test('415351108140.556')); 



explanation

  1. // start and end of expression
  2. ^ : everything that follows should be at the beginning of the line you are testing
  3. \d+ : there must be at least one digit
  4. ( )? : this part is optional
  5. \. : here comes the point
  6. \d{1,2} : there must be one to two digits
  7. $ : everything that precedes this should be at the end of the line you are testing



Tip

You can use regexr.com or regex101.com to test regular expressions right in your browser!

+12
Feb 12 '18 at 9:48
source share

Numbers with no more than two decimal places:

 /^\d+(?:\.\d{1,2})?$/ 

This should work fine. Try it :)

+3
Dec 10 '13 at 9:54 on
source share

See my cross-browser filter project for the value of a text input element on your web page using the JavaScript language: Input Key Filter . You can filter the value as an integer, a floating-point number, or write your own filter, for example, a phone number filter. See an example of a user-defined filter for inputting a floating-point number with a decimal pointer and restrictions to 2 digits after the decimal pointer:

 <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Input Key Filter Test</title> <meta name="author" content="Andrej Hristoliubov anhr@mail.ru"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <!-- For compatibility of IE browser with audio element in the beep() function. https://www.modern.ie/en-us/performance/how-to-use-x-ua-compatible --> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <link rel="stylesheet" href="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.css" type="text/css"> <script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/Common.js"></script> <script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.js"></script> </head> <body> <h1>Float field</h1> <input id="Float" onchange="javascript: onChangeFloat(this)" onblur="inputKeyFilter.isNaN(parseFloat(this.value), this);" /> <script> function CreateFloatFilterCustom(elementID, onChange, onblur){ try{ inputKeyFilter.Create(elementID , onChange , function(elementInput, value){//customFilter if(value.match(/^(-?\d*)((\.(\d{0,2})?)?)$/i) == null){ inputKeyFilter.TextAdd(isRussian() ? " : -[0...9].[0...9]  -[0...9]e-[0...9]. : -12.34 1234" : "Acceptable formats: -[0...9].[0...9] or -[0...9]e-[0...9]. Examples: -12.34 1234" , elementInput); return false; } return true; } , onblur ) } catch(e) { consoleError("Create float filter failed. " + e); } } CreateFloatFilterCustom("Float"); function onChangeFloat(input){ inputKeyFilter.RemoveMyTooltip(); var elementNewFloat = document.getElementById("NewFloat"); var float = parseFloat(input.value); if(inputKeyFilter.isNaN(float, input)){ elementNewFloat.innerHTML = ""; return; } elementNewFloat.innerHTML = float; } </script> New float: <span id="NewFloat"></span> </body> </html> 

Also see my page example input key filter

+2
Jul 31 '15 at 4:06
source share

I found that I can use

 ^\d+(\.\d+)?$ 

for more than two decimal places.

+1
Apr 07 '16 at 19:41
source share

Try regex:

 (?=[^\0])(?=^([0-9]+){0,1}(\.[0-9]{1,2}){0,1}$) 

Allowed: 1, 10.8, 10.89, 0.89, 0.89, 1000

Not allowed: 20., 50.89.9, 12,999,., Zero character Please note that this works for positive numbers

+1
Aug 05 '16 at 6:50
source share

compared to gven's answer to the microphone ... it doesn't check anything on some platforms I'm working on ... more precisely, it really doesn't work in Dream Viewer ..

real .. I will record it again ... which will work on any platform. "^ [0-9] + (. [0-9] {1,2})? $" .. thnkss ..

0
Jul 29 '13 at 16:35
source share
  function CheckValidAmount() { var amounttext = document.getElementById('txtRemittanceNumber').value; if (!(/^[-+]?\d*\.?\d*$/.test(amounttext))){ alert('Please enter only numbers into amount textbox.') document.getElementById('txtRemittanceNumber').value = "10.00"; } } 

This is a function that will take a decimal number with any number of decimal places and without decimal places.

Thank... :)

0
Sep 06 '16 at 18:39
source share

Hope this solves your problem! Custom directive to display only the required decimal places

Here, this directive takes only a number and a dot for a text value of input type when the user is not updated in the view more than the specified decimal places.

0
Jan 31 '19 at 17:23
source share

Schema for passing a value as a string. The regular expression checks the string consisting of at least one digit, followed by possibly a period and exactly two digits:

 { "type": "string", "pattern": "^[0-9]+(\\.[0-9]{2})?$" } 

The diagram below is equivalent, except that it also allows blank lines:

 { "type": "string", "pattern": "^$|^[0-9]+(\\.[0-9]{2})?$" } 
0
Jun 22 '19 at 8:11
source share

It works?

 [0-9]{2}.[0-9]{2} 
-3
Apr 05 2018-12-12T00:
source share



All Articles