Currency regex in Angular - regex

I need Help for currency regex in Angular.
I'm not really good at regex.
What I want is a regex that:
allows comma as digital-group-separator, but not in the beginning or the end.
allows only 2 digits rounded after decimal point.
allows only one decimal point and not in the beginning or the end.
not allows 0.00 or 0.
This is my regex:
(?=.*?\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|\d+)?(\.\d{1,2})?$
but this regex allows 0.00
any one here please help thanks
Desired Outputs
Valid:
1,000.00
1000
0.01
24
1,234,000
11,222,245.22
Not Valid:
,000.00
,,,,,9
0
0.00
1,22,2,
1,000.
123,123,22
000,300.00
000300.00
000,123
000,000
00,000
0,000

You may use
/^(?![0,.]+$)(?:0|[1-9]\d{0,2}(?:,\d{3})*|[1-9]\d*)(?:\.\d{1,2})?$/
See the regex demo
Details
^ - start of string
(?![0.,]+$) - a negative lookahead that fails the match if there are one or more 0, , or . chars till end of string
(?:0|[1-9]\d{0,2}(?:,\d{3})*|[1-9]\d* - either of the three alternatives:
0 - zero
| or
[1-9]\d{0,2}(?:,\d{3})* - one to three digits with the first one being non-zero, and then 0 or more repetitions of a comma and then three digits
| - or
[1-9]\d* - 1+ digits with the first one being non-zero,
(?:\.\d{1,2})? - an optional sequence of a . and then 1 or 2 digits
$ - end of string.

Related

RegEx for matching numbers from 0.00 to 50.00

How can I get an regex expression that allows:
0.00 to 50.00 and also comma as decimal separator so
0,00 to 50,00
I have gotten as far as ([0-5]{1})?([0-9]{1})([,][0-9]{1,2})?
but there are still situations in which it fails. I have searched on line but could not find the right answer.
ADDED:
A small change in requirements. Actually it should be from 0.01 to 50.00, and 0,01 to 50,00. (But with the answers below I think I managed to adapt the regex strings so this is also matched)
There is a regex for range generator here. Generated and played with it, came up with this
^(?:[1-4]?\d[.,]\d\d?|50[.,]00?)$
Added non capturing group, little modifications and ^ start / $ end anchor. See demo at regex101.
For optional decimal part, how about this: ^(?:[1-4]?\d(?:[.,]\d\d?)?|50(?:[.,]00?)?)$
This regex should cover almost all cases matching numbers from 0.00 to 50.00,
^(?=.)(?:(?:(?:0|[1-4]?\d)?(?:[,.]\d{1,2})?)|50(?:[.,]00?)?)$
Explanation:
^ - Start of string
(?=.) - Positive look ahead to avoid matching empty string
(?: - Start of first non-grouping pattern
(?: - Start of second non-grouping pattern
(?:0|[1-4]?\d)? - This matches whole number from 0 to 49 and this whole number could be absent
(?:[,.]\d{1,2})? - Matches a comma or dot followed by one or two digits and this decimal part can be absent
) - Closing of second non-grouping pattern
| - Alternation for number 50 case
50(?:[.,]00?)?) - Matches 50 followed by either comma or dot followed by 0 or 00 where decimal part is optional
) - Closing of first non-grouping pattern
$ - End of string
Regex Demo
Edit:
For discarding zero value numbers, you can just add a negative lookahead (?!0*[.,]?0*$) in current regex and use this regex,
^(?=.)(?!0*[.,]?0*$)(?:(?:(?:0|[1-4]?\d)?(?:[,.]\d{1,2})?)|50(?:[.,]00?)?)$
Regex Demo rejecting zero valued numbers
does this one work for you?
(50([.,]0{1,2})?)|([0-4]?[0-9]([.,][0-9]{1,2})?)

A RegEx that matches correct comma placement in a string

I am wanting to check user input to where it check whether a string has correct comma placement, and if the number is valid in human eyes too.
These are numbers that are allowed:
1,000
100
1
1,000,000,000,000,000
Here are numbers that are not allowed:
1e+5
1e5
1,00
-105
100.50
100,00,00,0,000000
I've tried to come up with my own RegEx but this is very complicated for even me to understand. This is my RegEx (^[0-9]{0,3}(,[0-9]*)?$) but it is very broken at the moment.
Is anyone able to help?
You may use
^\d{1,3}(?:,\d{3})*$
See the regex demo
Details
^ - start of string
\d{1,3} - 1, 2 or 3 digits
(?:,\d{3})* - zero or more consecutive occurrences of
, - a comma
\d{3} - 3 digits
$ - end of string.

Regex that does not allow zeros

I use this regex:
/^(?!0000)(?!0+(?:[.,]\d{1,2})?$)\d{1,4}(?:[.,]\d{1,2})?$/
It allows decimal numbers (. and , separators), with two digits after the separator. It does not allow zero values. What I want to do is to make it allow numbers like 0.1 and 0.09 etc... Here it's impossible to write any number starting with 0. I don't know how to do this. Any idea?
Thanks.
You can simplify your regex with just one negative lookahead:
/^(?![,.0]*$)\d{1,4}(?:[.,]\d{1,2})?$/gm
RegEx Demo
(?![,.0]*$) will prevent any input with just 0s, dots or commas in input.
Any regex based approach would be trivial to bypass. For example, they could use any of the following to evade your regex filter:
+0- Start with +
-0 - Start with -
0e0 - Scientific notation
1e-999 - Not real 0, but most likely will be one after conversion.
And any combination of methods above.
Long story short: regex wouldn't work here.
Try to cast your string to a number and reject if it equals to 0.
You should be able to do that in most languages, including JavaScript.
Since you mentioned JS/Angular, you may replace all , with . (in case the decimal separator is ,) and cast the string to a number to check if it is zero, and if it is, then use your simplified regex /^\d{1,4}(?:[.,]\d{1,2})?$/ that makes sure there are 1 to 4 digits in the whole part and 1 to 2 digits in the fractional part:
function check(str) {
str = str.replace(/,/g, ".");
if ( parseFloat(str) > 0 ) {
return /^\d{1,4}(?:\.\d{1,2})?$/.test(str);
}
return false;
}
console.log(check("0,00"));
console.log(check("0,09"));
console.log(check("900000"));
If you cannot access the code, adjust the negative lookahead like
^(?!0+(?:[.,]0+)?$)\d{1,4}(?:[.,]\d{1,2})?$
See the regex demo
The (?!0+(?:[.,]0+)?$) negative lookahead fails the match only if the string starts with 1 or more zeros, and then has an optional sequence of , or . followed with 1 or more zeros.
^ - start of a string
(?!0+(?:[.,]0+)?$) - reject the match if it matches a sequence of
0+ - 1 or more zeros
(?:[.,]0+)? - an optional sequence of . or , followed with 1 or more zeros
\d{1,4} - 1 to 4 digits
(?:[.,]\d{1,2})? - an optional sequence of:
[.,] - a . or ,
\d{1,2} - any 1 or 2 digits
$ - end of string.
The lookahead pattern may be reduced to (?!0*[.,]?0+$), since the consuming pattern will ensure the correct format is matched.

Regex range between 0 and 100 including two decimal

I'm trying to figure out a regex expression that does the following. Both conditions below must be true:
1) Between 0 and 100 inclusive
2) Can contain one or two decimals only but not obligatory.
It should not allow 100.01 or 100.1
100 is the maximum value, or 100.0 or 100.00
I tried ^(100(?:\.00)?|0(?:\.\d\d)?|\d?\d(?:\.\d\d)?)$
which helped me in this question
but this does not accept 99.0 (one decimal).
I'm probably very close.
You just need to make each second decimal digit optional:
^(?:100(?:\.00?)?|\d?\d(?:\.\d\d?)?)$
^ ^
See the updated regex demo. The 0(?:\.\d\d)? alternative is covered by \d?\d(?:\.\d\d)? one (as per Sebastian's comment) and can thus be removed.
The ? quantifier matches one or zero occurrences of the subpattern it quantifies.
Pattern details:
^ - start of string
(?: - start of an alternation group:
100(?:\.00?)? - 100, 100.0 or 100.00 (the .00 is optional and the last 0 is optional, too)
\d?\d(?:\.\d\d?)? - an optional digit followed by an obligatory digit followed with an optional sequence of a dot, a digit and an optional digit.
) - end of the alternation group
$ - end of string.
BONUS: If the number can have either . (dot) or , (comma) as a decimal separator, you can replace all \. patterns in the regex with [.,]:
^(?:100(?:[.,]00?)?|\d?\d(?:[.,]\d\d?)?)$

Regex to match currency value (with decimal 2 digits) but not 0

I want to match:
0.01
0.12
1.00
1.12
12.00
12.34
But not:
0
0.00
.00
.12
To get price value which can not be zero.
I have written this regex:
/(^\d{1,}\.\d{2}$)|(^\d{1,}$)/
But it matches:
0
0.00
which I want to eliminate.
I've also tried with exclusion rules like ^(?!0)(?!0\.00)\d{1,}\.\d{2}|\d+$ but no success.
You want to match a value with decimal 2 digits but not 0:
^(?![.0]*$)\d+\.\d{2}$
The negative lookahead checks, if the string does not consist of only 0 and .
\d+\.\d{2} assures the desired formatting: One or more digits followed by a . followed by 2 digits.
Test at regex101.com (also see explanation on the right side)
To also allow 1, 2.2 change the last part to \.\d{1,2} and make it optional:
^(?![.0]*$)\d+(?:\.\d{1,2})?$
Test at regex101.com
(?=.*?[1-9])(^\d+\.\d{2}$)|(?=.*?[1-9])(^\d{1,}$)
Try this.Just added (?=.*?[1-9]) to negate 0.See demo.
http://regex101.com/r/yA1jY6/1
You could try the below regex.
^(?!0(\.00)?$)\d+(?:\.\d{2})?$
DEMO
Negative lookahead at the start won't allow 0 or 0.00