Regular Expression to accept only positive numbers and decimals
/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/
matches
0
+0
1.
1.5
.5
but not
.
1..5
1.2.3
-1
EDIT:
To handle scientific notation (1e6
), you might want to do
/^[+]?([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$/
If you want strictly positive numbers, no zero, you can do
/^[+]?([1-9][0-9]*(?:[\.][0-9]*)?|0*\.0*[1-9][0-9]*)(?:[eE][+-][0-9]+)?$/
Regular Expression to accept only positive number and decimals (only end with .5 .0 or integer accepted)
You can check this here at regex101. The regex I used to do this is:
(^0\.5$)|(^[1-9][0-9]*(\.[05])?$)
What is highlighted in blue is a match.
Regular expression for positive decimal number with 0, 1 or 2 decimal places
Edit 2: now disallows exactly 0,0.0, etc.
This matches at least one digit before the decimal place, followed by an optional decimal place, followed by 0-2 digits.
The negative lookahead looks for any flavor of absolute zero and prevents a match.
^(?!0*[.,]0*$|[.,]0*$|0*$)\d+[,.]?\d{0,2}$
This is the raw regex, so you'll need to escape it appropriately for your language. (For example, in some languages you need to double the \
slashes as \\
.
/^(?!0*[.,]0*$|[.,]0*$|0*$)\d+[,.]?\d{0,2}$/
RegEx that matches positive numbers
This would be my way: ^[+]?\d+([.]\d+)?$
EDIT: If you want to allow something like .23
, you could use ^[+]?([.]\d+|\d+([.]\d+)?)$
EDIT: tchrist insists on this one: allowing 4.
, you could use ^[+]?([.]\d+|\d+[.]?\d*)$
Explanation:
- with or without positive sign
- one or more digits
- with or without:
- decimal point
- one or more digits
Note: This will not accept a negative number, which is what you ultimately want.
Regex for only positive decimals from 0 to 1 upto 2 decimal digits only that works without sign in JavaScript?
You may use:
^(?:0(?:\.[0-9]{1,2})?|1(?:\.00?)?)$
RegEx Demo
RegEx Details:
^
: Start(?:
: Start a non-capture group0
: Match0
(?:\.[0-9]{1,2})?
: Match optional dot followed by 1 or 2 digits|
: OR1
: Match1
(?:\.00?)?
: Match optional 1 or 2 zeroes after dot
)
: End non-capture group$
: End
Related Topics
How to Prevent Form to Reload Page After Onsubmit
Html Input File Selection Event Not Firing Upon Selecting the Same File
Bootstrap Slides Lagging While Using Multi Item Carousel Using Angular 6
Removing Number from Notification Icon
Method for Splitting String into Separate Cells in Sheets/App Script
How to Remove Errors Warnings from Console
Handling Multiple Http Responses One by One in Angular 6
How to Pass Get Request to Window.Location in JavaScript
Show Questions One At a Time in Quiz Website in HTML
How to Get First N Number of Elements from an Array
Typescript | Warning About Missing Return Type of Function, Eslint
Remove the Default Browser Header and Footer When Printing HTML
Center Content Vertically on Vuetify
What Is the Best Method to Swap Words in a String
Retrieving Keys from Json Array Key-Value Pair Dynamically - JavaScript
Why Does This Onclick Event Not Work in an HTML Iframe
How to Get Full Path of Selected File on Change of <Input Type='File'> Using Javascript, Jquery-Ajax