Regex to check amount is greator then one - javascript

I have a regex
^(?=.*[1-9])\\d{0,5}(?:\\.\\d{0,2})?$
to check amount should be greater then 0 and it can contain maximum five digit before decimal and optional decimal and 2 digits after decimal.
Here (?=.*[1-9]) is to check there should be latest one occurrence of any digit from (1-9) in the string.
But I want modify it to check amount greater then one(1), so I want (?=.*[1-9]) to check only till the occurrence of the decimal point i.e. 0.1 it should return false.
Other condition should also fulfill.
Note : the count of digits before decimal is not fix, it will very from 1 to 5, so we can't modify it to (?=.{0,5}[1-9])

You can fix it by using this negation based regex:
^(?=[^.]*[1-9])\\d{0,5}(?:\\.\\d{0,2})?$
RegEx Demo
[^.]* before [1-9] will match any character except decimal point thus not allowing 0.45 as valid number.

Related

Javascript regex positive less than 10000 than can be decimal

I need a regex for a number than can be decimal and less than 10000 with max 2 digits after decimal.
I tried
/^([0-9]{1,4})+(\.[0-9]{0,2})$/
but it returns true for 44555.54 for example.
In your regex you are using + which is using for one or more repetition and which leads to match any length of digit and make decimal part non-greedy(using?) to make it optional.
^[0-9]{1,4}(\.[0-9]{0,2})?$
or using \d for digit character class.
^\d{1,4}(\.\d{0,2})?$

Regex for non negative and non zero for the format ###.##

I have a requirement to validate some inputs which should be in format ###.##
Invalid inputs are:
-11.10 ==> no negative
000.00 or 0 ==> 0 not allowed should be positive
Valid inputs are:
1
11
111
1.1
11.11
111.11
I have tried with the following regex ^([^-]\d{0,2}(.\d{1,2})?)$ which fulfills my requirements except it's accepting 0 which I don't want. How I can modify my regex so only 0's do not get matched?
Thanks For Help
Try
^(?=.*[1-9])\d{1,3}(?:\.\d\d?)?$
It should do it for you.
It starts with a positive look-ahead to make sure there's a digit other than 0 present.
Then it matches 1-3 digits, optionally followed by a . and 1-2 digits.
Your regex101 updated.
([0-9]){1,3}(\.[0-9]{1,2})? is the expression you are searching for.
([0-9]){1,3} = any number sequence with a length from 1 up to 3
(\.[0-9]{1,2})? = "?" maybe there is a float tail
(\.[0-9]{1,2}) = float tail must start with a dot and decimal numbers have to be up to 2
There is a way to except all zero sequences but it will waste your time for no reason, as you can simply check it with if myNum > 0.
It will work for negative values also, but this regex excludes them too.
^[1-9][0-9]*(\.[0-9]+)?$|^0\.[0-9]+$
This will work for you. It accepts all valid positive decimal numbers excluding 0.

Match float RegExp

I need to match a float number from 0 to 20.
All of those must be valid:
1.20
01.20
12.00
13,59
11
1
08
00.50
5,50
But not those:
001,20
1.200
13,
13.
.50
To sum up.
I want both comma and dot separator
One or two digits before separator (with minimum value = 00 and maximum value = 20)
zero, one, or two digits after separator.
I have tried a lot of patterns but no one matches exactly.
^([01]?\d)([,.]\d?\d)?$|^20([,.]0?0)?$
Matches everything from 0 to 20.
See it in action
The idea is:
^$ - from the start til the end of the string
([01]?\d) - the part before the decimal point can be a single digit number (\d) or have two digits, where the first is 0 or 1 ([01]?)
[,.] - decimal point, which is either a point or a comma
\d?\d - one or two digits
()? - the floating part is optional
| - either the above is matched, or..
20 - simply 20
([,.]0?0)? - with optional decimal part, which consists of decimal point with one or two zeroes

Regex for number with decimals and thousand separator

I need regex to validate a number that could contain thousand separators or decimals using javascript.
Max value being 9,999,999.99
Min value 0.01
Other valid values:
11,111
11.1
1,111.11
INVALID values:
1111
1111,11
,111
111,
I've searched all over with no joy.
/^\d{1,3}(,\d{3})*(\.\d+)?$/
About the minimum and maximum values... Well, I wouldn't do it with a regex, but you can add lookaheads at the beginning:
/^(?!0+\.00)(?=.{1,9}(\.|$))\d{1,3}(,\d{3})*(\.\d+)?$/
Note: this allows 0,999.00, so you may want to change it to:
/^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?$/
which would not allow a leading 0.
Edit:
Tests: http://jsfiddle.net/pKsYq/2/
((\d){1,3})+([,][\d]{3})*([.](\d)*)?
It worked on a few, but I'm still learning regex as well.
The logic should be 1-3 digits 0-1 times, 1 comma followed by 3 digits any number of times, and a single . followed by any number of digits 0-1 times
First, I want to point out that if you own the form the data is coming from, the best way to restrict the input is to use the proper form elements (aka, number field)
<input type="number" name="size" min="0.01" max="9,999,999.99" step="0.01">
Whether "," can be entered will be based on the browser, but the browser will always give you the value as an actual number. (Remember that all form data must be validated/sanitized server side as well. Never trust the client)
Second, I'd like to expand on the other answers to a more robust (platform independent)/modifiable regex.
You should surround the regex with ^ and $ to make sure you are matching against the whole number, not just a subset of it. ex ^<my_regex>$
The right side of the decimal is optional, so we can put it in an optional group (<regex>)?
Matching a literal period and than any chain of numbers is simply \.\d+
If you want to insist the last number after the decimal isn't a 0, you can use [1-9] for "a non-zero number" so \.\d+[1-9]
For the left side of the decimal, the leading number will be non-zero, or the number is zero. So ([1-9]<rest-of-number-regex>|0)
The first group of numbers will be 1-3 digits so [1-9]\d{0,2}
After that, we have to add digits in 3s so (,\d{3})*
Remember ? means optional, so to make the , optional is just (,?\d{3})*
Putting it all together
^([1-9]\d{0,2}(,?\d{3})*|0)(\.\d+[1-9])?$
Tezra's formula fails for '1.' or '1.0'. For my purposes, I allow leading and trailing zeros, as well as a leading + or - sign, like so:
^[-+]?((\d{1,3}(,\d{3})*)|(\d*))(\.|\.\d*)?$
In a recent project we needed to alter this version in order to meet international requirements.
This is what we used: ^-?(\d{1,3}(?<tt>\.|\,| ))((\d{3}\k<tt>)*(\d{3}(?!\k<tt>)[\.|\,]))?\d*$
Creating a named group (?<tt>\.|\,| ) allowed us to use the negative look ahead (?!\k<tt>)[\.|\,]) later to ensure the thousands separator and the decimal point are in fact different.
I have used below regrex for following retrictions -
^(?!0|\.00)[0-9]+(,\d{3})*(.[0-9]{0,2})$
Not allow 0 and .00.
','(thousand seperator) after 3 digits.
'.' (decimal upto 2 decimal places).

Javascript regex for allowing only positive digits

I use this to test for digits
/^\d+$/
But I need to make sure that it's greater than zero, while still allowing 0000123123123 for instance.
You can write:
/^\d*[1-9]\d*$/
(zero or more digits, followed by a nonzero digit, followed by zero or more digits).
It is correct regex for positive digits.
/^[1-9]\d*$/g
The previous answer not correct for 0123.

Categories