Regular Expression to accept only positive numbers and decimals - javascript

I need a regular expression in javascript that will accept only positive numbers and decimals. This is what I have but something is wrong -- it doesn't seem to take single positive digits.
/^[-]?[0-9]+[\.]?[0-9]+$/;
For example, 9 will not work. How can I restructure this so if there is at least one positive digit, it will work?

/^[+]?([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]+)?$/

There are few different ways to do this depending on your need:
/^[0-9.]+$/ matches 1 and 1.1 but not -1
/^[0-9]+\.[0-9]+$/ matches 1.1 but not 1 or -1
Generally, I recommend using a simple regExp reference guide like http://www.regular-expressions.info/ for building expressions, and then test them using javascript right your browser console:
"123.45".match(/^[0-9.]+$/)

You can try this -
^\d{0,10}(\.\d{0,2})?$
Also one cool site to test as well as to get description of your own regular expressions
https://regex101.com/

How about like:
^[.]?[0-9]+[.]?[0-9]*$

I have found something interesting which work for me!!!
hope so for you also...!!!
[0-9].[0-9]
Regex for accepting only a positive number with a decimal point
Matches:
1, 2564, 2.5545, 254.555
Not Matches:
-333, 332.332.332, 66-665.455, 55554-4552

I have done one in case people are interested:
^([0-9]*)(.[[0-9]+]?)?$
but it doesn't have '+' sign
while also it doesn't accept '-' sign
It accepts:
0
0.5
1.0
1.5
2.0
2.5
3.0
1
2
3
4
0.0
0.1
0.9
0.4
1.3
1.11
1.51
2.01
01.5
0.51
1.1
.1
Reject:
-1.0
-0.5
-0.0
-0.1
-0.7
11123.
1.001.
.1.1

Related

Regex allow zero and one decimal point at the beginning or between numbers [duplicate]

I want to modify my existing Regular Expression which accepts decimals from 0 to 99.99
\d{0,2}(\.\d{1,2})?$
i want this to be accept
100
100.0
100.00
and should not accept
100.1
100.02
101
Can anyone help me modify the above RE
I guess it's best to add the test for 100 as a special case using |:
^(\d{0,2}(\.\d{1,2})?|100(\.00?)?)$
Use Floating-Point Comparisons Instead
You already have answers for doing this with a regular expression, but it's usually more efficient to handle this as a floating-point comparison with boundary conditions. For example, using Ruby:
number = 99
number.to_f >= 0 and number.to_f <= 100
=> true
number = 100.01
number.to_f >= 0 and number.to_f <= 100
=> false
In these examples, the variable is cast as a float so that strings and integers are compared properly, and then the float is compared to the boundary conditions of zero and 100. It's quick, easy to write, and (most importantly) easy to read.
Your mileage may vary.
Worked for me from 00.00 to 99.99
/^(?=.*\d)\d{0,2}(?:\.\d{0,2})?$/
100(\.0{1,2})?$|\d{0,2}(\.\d{1,2})?$
(100(\.[0]{1,2})?|[0-9]{1,2}(\.[0-9]{1,2})?)
I think, this will help you "^(100(.0{0,2}?)?$|([1-9]{0,1})([0-9]{1})((.[0-9]{0,2})|(\,[0-9]{0,2}))?)$"
It will match everything between this:
0-100,
0.0 - 100.0,
0.00 - 100.00
and all this with comma.
^(0*(\d{1,2}(\.\d+)?)|\.\d+|100(\.0+$)?)$
This will apply to any values between 0-100 with decimals that are valid for most programming languages
valid: 0, 0.2, .1, 0001.020, 9, 010, 100.0000
invalid: -1, 100.010, 1., ., empty

Regex expression for -10.01 to10.12 including 0

I am looking for regex expression which takes -10 to 10 with 2 decimal numbers upto 12. means cases passed are -10.01 to -10.11 for 10 years and 11 months.
I was able to make a regex for -10 to 10 but adding decimal till .11 was not able to figure out If somebody can help that would be great.
cases passed -
0
1
10.01
10.00
10.11
-10.00
-10.01
10.12
-10.00
10
cases should fail -
10.12
10.13
-10.13
11
-11
-10.13
I presume that values such as 9.08 and -5.04 are also valid? In that case this regex: ^-?(10|[0-9])(\.(0[0-9]|1[01]))?$ will match the values you want. The first part looks for a number from -10 to 10; the second part looks for an optional decimal part which can be something from 00-09 or 10 or 11.
Regex101 demo

Javascript Floating Point Number Regex Expression

I am looking for a regular expression to match floating point numbers with at most 4 digits before the '.' and at most 4 digits after.
Examples of valid inputs:
1234.5678
123.567
12.34
1.2
1.23
12.3
etc...
There are many places where you get the answer for it. Here is your required answer,
^\d{0,4}(\.\d{0,4})?$
Test the answer in this Link

Regular expression for decimal “X.X”

I’m trying to figure out the regular expression for “X.X”, where ‘X’s are numbers.
Some valid values:
1.1
2.0
0.1
9.1
9.9
Some invalid values:
10.1
9.22
1
10
Looking at your examples this regex should work:
/^\d\.\d$/
Live Demo: http://www.rubular.com/r/vdemgjS9NY
I'm no regex expert,
but "^\d.\d$"
seems to work for me when I test on Rubular for a line with only X.X and nothing more.

Allowing money amounts to be entered into an input with a JavaScript Regex

I'm using plain vanilla JavaScript and need some help with my regex. Money in the following formats has to be allowed, and in these formats only (with no limit on the number of 0s (tens, hundreds, thousands, etc.) for the dollar amounts allowed):
$25,000
$25000
25,000
25000
25000.01
25,000.99
2000.99
50.00
50
1.95
1 .99
0.25
$0.25
0.2
2.3
2000.5
.75
var regex = /^\$?.?[1-9][0-9,]*(.[0-9]{0,2})?$/;
Currently, it's not allowing amounts like 0.99 to be entered.
Try this
^\$?(?:\d+|\d{1,3}(?:,\d{1,3})*)(?:\.\d{2})?$
See it here on Regexr
The only thing that is is not matching is your third last example, it has a space before the dot. Is that valid?
Edit:
My frist solution has the restriction, that it would accept numbers starting with 0, like 001. This solution uses a negative lookahead to avoid this:
^\$?(?!0\d)(?:\d+|\d{1,3}(?:,\d{1,3})*)(?:\.\d{2})?$
See it here on Regexr
Solution without lookahead
^\$?(?:0|[1-9]\d*|[1-9]\d{0,2}(?:,\d{1,3})*)(?:\.\d{2})?$
See it on Regexr
^\$?(?:[1-9]\d?\d?(?:(?:,\d{3})*|(?:\d{3})*)|0)(?:\.\d\d?)?$
Altho I wouldn't use such strict input restrictions.

Categories

Resources