RegEx for a number that must contain a decimal point - javascript

I am trying to write a regex to validate a decimal number.
The number MUST contain a decimal point.
There must be one digit [0-9] to the right of the decimal point.
There may be max 5 digits to the left of the decimal point but they cannot start with zero unless a zero on its own.
Allowed:
0.1 (first digit can only be zero if followed by the decimal place)
10345.0 (max 5 digits to left of decimal place, there must be 1 digit to right of decimal place)
Not allowed:
0 (no decimal place)
10 (no decimal place)
(no number to the right of the decimal place)
. (no number on either side of the decimal place)
.1 (no number to the left of the decimal place)
This regex validates what comes before the decimal place
^(?:[1-9][0-9]{0,4}|0)$
This regex validates what comes after the decimal place
^[0-9]{1}+$
I just don't know how to combined the two such that the decimal place is mandatory.
How do I solve this problem?

To the best of my knowledge, this works
^(?:[1-9]\d{0,4}|0)\.\d$
Expanded
^ # BOS
(?:
[1-9] \d{0,4} # 1-5 digits, must not start with 0
| # or,
0 # 0
)
\. \d # decimal point and 1 digit
$ # EOS

This RegEx might validate your inputs:
^(\d{1,5}\.\d{1,})$
and you can simply call it using $1.

That should work
const r = /^(?:[1-9]\d{0,4}|0)\.\d+/
const arr = ['0', '1', '1.2', '1.', '1.123', '0123.1', '123.123', '1234.1', '12345.12345678', '123456.123', '0.12'];
arr.forEach((val) => {
console.log(val, r.test(val));
})

Related

regular expression no more than one digit

how to make a regular expression into one number (only numbers) and that it does not exceed 10 from 0 to 10
/^[1-9][1]*$/.test(message)
It doesn't work that way for me.
To specify the amount of a specific character use {} instead of [], in this case, as it is only one digit, you do not need to specify a count as 1 is default:
/^[0-9]$/.test(message)
I assume you mean you want to match a single digit between 0 and 10. If not please comment to clarify.
Hope this helps.
Assuming you only want integers or whole numbers, then use:
/^(?:[0-9]|10)$/
If you want to allow for decimals, then use:
/^(?:[0-9](?:\.\d+)?|10(?:\.0+)?)$/
The second regex says to match:
^ from the start of the number
(?:
[0-9] 0 to 9
(?:\.\d+)? any optional decimal component
| OR
10 match integer 10
(?:\.0+)? optional zero decimal only
)
$ end of the number

Regex for only positive decimals from 0 to 1 upto 2 decimal digits only that works without sign in JavaScript? [duplicate]

This question already has answers here:
Regex decimal values between 0 and 1 up to 4 decimal places
(2 answers)
Closed 2 years ago.
I basically want a regex for alpha in RGBA which is always a positive number between 0 & 1. However, I want it to only be upto 2 digits like 0.53 & not more than that like 0.536.
Allowed
Anything between 0 to 1 but only upto 2 decimal places
0
0.0
0.00
0.1
0.12
0.34
1
1.0
1.00
Not Allowed
Anything outside of 0 to 1 & if its between 0 to 1 then it should be less than or equal to 2 decimal places only & even signs not allowed
0.123
90
3
-1
+1
I noticed other similar questions but they allow signs or they allow more than 2 decimal places.
Currently, I have a regex like /^(0+\.?|0*\.\d+|0*1(\.0*)?)$/ which allows for more than 2 decimal places. How do I solve it?
You may use:
^(?:0(?:\.[0-9]{1,2})?|1(?:\.00?)?)$
RegEx Demo
RegEx Details:
^: Start
(?:: Start a non-capture group
0: Match 0
(?:\.[0-9]{1,2})?: Match optional dot followed by 1 or 2 digits
|: OR
1: Match 1
(?:\.00?)?: Match optional 1 or 2 zeroes after dot
): End non-capture group
$: End
Alternatively, try:
^(?!1..?[1-9])[01](?:\.\d\d?)?$
See the online demo
^ - Start string anchor.
(?! - Open negative lookahead:
1..? - A literal "1" followed by any character other than newline and an optional one.
[1-9]- Match a digit ranging from 1-9.
) - Close negative lookahead.
[01] - Match a zero or one.
(?: - Open non-capture group:
\.\d\d? - Match a literal dot, a single digit and an optional one.
)? - Close non-capturing group and make it optional.
$ - End string anchor.

regex for age with 2 decimal points

What is regex for age with 2 decimal points value with max value 99.11
Valid examples:
1
12.01
16.06
20.11
Invalid examples:
.0
1.12 (decimal value should be <12)
1.13 (decimal value should be <12)
12.111
100.00 (2 digit before decimal point, max 99.11)
I have tried ^[0-9]\d{0,1}(\.\d[0-1]\d{0,1})*(,\d+)?$ regex but it is not following all criteria.
can anyone help me?
You can try with this pattern:
/^\d{1,2}(\.(0[0-9]|1[01]))?$/
var pattern = /^\d{1,2}(\.(0[0-9]|1[01]))?$/;
console.log(pattern.test('1'));
console.log(pattern.test('12.01'));
console.log(pattern.test('16.06'));
console.log(pattern.test('20.11'));
console.log(pattern.test('.0'));
console.log(pattern.test('1.12'));
console.log(pattern.test('1.13'));
console.log(pattern.test('12.111'));
console.log(pattern.test('100.00'));
Try this.
/^\d\d?(\.(0\d|1[01]))?$/
Description:
^\d\d? - from the start of the string, match 1 or 2 numbers
After that/those numbers, optionally match . plus 2 numbers
The two numbers must be 0 and any number, else 1 and 0 or 1
Anchor to the end of the string
You are missing the two different patterns of floating part, which must be handled with a |.
A character-optimal solution is ^\d{1,2}(\.[0]\d|\.1[01])?$.

Regular expression for length limit 10 digit before decimal till 2 decimal place with Number only?

i want a regular expression for length limit 10 digit with 2 decimal place numbers with only numbers allowed.10 digit before decimal
allowed
1
1111111111
111
1111111111.22
.2
1.2
1.22
Not allowed
4.
.
-1
abc
Null
Emptystring
""
1.222
-1.22
111111111111
tried but not working
^[0-9]*(\.[0-9]{0,2})?$
You're almost there - all you have to do is include a check that the string is not empty (which can be accomplished with a positive lookahead for .{1,10} right after the ^), and check that its first digit string has at most 10 characters (just use a {0,10} quantifier for the digits). Also note that [0-9] simplifies to \d.
In order to also exclude trailing dots, repeat the digit after the dot with {1,2} instead of {0,2}:
^(?!$)\d{0,10}(?:\.\d{1,2})?$
https://regex101.com/r/Ah8dNu/5
I have also prepared below RegEx.
Seems this will also work.
^[0-9]{1,10}((\.)[0-9]{0,2}){0,1}$

currency regex with decimal allowed with 0

With reference to my previous question currency regex
I want to add an condition, i want to allow decimal only if it starts with 0
example
0.25 should be allowed
1.25 not allowed
current regex is as following
/^(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?$/
which matches comma trailing etc.
This will match all the currency matches from before without decimals or decimals with 0.x*
/^((?:\d{1,3}(?:,\d{3})+|\d+)|(?:0\.\d+))$/
If you only want to match 0.xx instead of an arbitrary number of decimal places use
/^((?:\d{1,3}(?:,\d{3})+|\d+)|(?:0\.\d{2}))$/
This one changes \d+, one or more digits, to \d{2}, exactly 2 digits.

Categories

Resources