currency regex with decimal allowed with 0 - javascript

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.

Related

How to use Regex to limit user input to numeric fields, with one allowed decimal and one allowed negative symbol in front

I am trying to only allow numeric values, along with a possible negative symbol at the front of the string, and a single decimal, using Regex in JavaScript. This input value will only allow for a possible 10 digits also.
Currently, when a user inputs text, I have this logic that will only allow numbers, negative signs, and decimals. However, I want to limit the number of the negative sign to one, and only allow this at the front of the string. Also, I want to limit the number of decimals to one.
input.slice(0, 10).replace(/[^0-9.\-]/, '');
Can anyone please help me figure this out?
You can do:
input.slice(0, 10).match(/([-]?)([0-9]+)([.]?)([0-9]?)/);
Short n sweet.
/^-?\d{0,10}(\.\d{0,3})?$/
optional negative char. at the start
allows max of 10 digits next
again optional decimal group with at most 3 decimal places
These barriers can be modified accordingly.

How to create a Regular Expression with the following conditions

Numerical input should not exceed 8 digits ( counting numbers on both the side of the decimal). Example: 123456.78 or 0.12 or 1.23
Numerical values should be able to accept negative numbers. Example: -0.8 , or -1.23
Max of 2 decimal number allowed
Numerical value should not accept any alpahbets in the input box
I tried Creating the following regular expression but i am not able to figure it out quite efficiently
^-?[0-9]\d*(\.\d+)?[,8]$
Regular exp
To match the required number of digits and no more, lookahead at the beginning of the pattern for (?:\d\.?) 1 to 8 times, and then match \d*(?:\.\d{1,2})?$ to match a number which, if containing decimals, contains at maximum 2 decimal characters:
^-?(?=(?:\d\.?){1,8}$)\d*(?:\.\d{1,2})?$
https://regex101.com/r/rQMRVX/5
(unless you need to capture the decimal part, it can be a non-capturing group like above)
You can try this
^-?\d{1,8}.?\d{1,2}$
Link : https://regex101.com/r/x7yw5M/2

Regex for validate string of int range

I am trying to build regex that can validate number or range input.
Allowed values are
Any number between 1 and 1816
A range consisting of 2 numbers separated by -. Each number must be between 1 and 1816. E.g. "1-1816", "3-100", "1815-1816"
Invalid values include
"0"
negative numbers (like "-13")
numbers with leading zeros (like "01")
numbers out of range 1-1816 (alone or as part of range)
Any regex will be fine JavaScript or C#.
So far I figured out just
(?<=\s|^)\d+(?=\s|$)
The problem with your regex is that you are accepting all digits via \d and not limiting it to suit your accepted range.
Use this:
^(?:181[0-6]|180\d|1[0-7]\d{2}|[1-9]|\d{2,3})(?:-(?:181[0-6]|180\d|1[0-7]\d{2}|[1-9]|\d{2,3}))?$
This regex limits the numbers to be in the range 1-1816, while supporting either individual numbers or a range via a hyphen separator as specified.
Demo
This regex should do the trick:
^([1-9][0-9]{0,2}|1[0-7][0-9]{2}|180[0-9]|181[0-6])(-([1-9][0-9]{0,2}|1[0-7][0-9]{2}|180[0-9]|181[0-6]))?$
Basically it allows
A number with leading 1 and 0 to 2 more digits
A number with leading 1, followed by 0-7 and twice any number
A number with leading 180 followed by one more number
A number with leading 181 followed by one number 0-6
This optionally once more repeated after -
But generally regexes are not good to work with numbers (and hard to update whne the number changes). If you have such option I'd rather just validate the number by much simpler regex, like
^[1-9][0-9]{0,3}(-([1-9][0-9]{0,3}))?$
and than programmatically split it by - (if present) and parse the individual segments as integer and validate their numeric value against the range.
That would additionally allow you to check for invalid ranges like 100-1, 1800-1800 etc.

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}$

Regex to allow negative values with maximum of 4 decimal places

I need a little help with changing the following regex to allow negative numbers. It needs to allow decimal numbers of up to a maximum of 4 decimal places, which it already does.
/^\d+(\.\d{1,4})?$/
Can anyone help?
Thanks
Just put -? before the first digit - "optionally match a minus sign"
Add the - in the character class with optional(?)
/^[-+]?\d+(\.\d{1,4})?$/
The following allows for integer numbers (123), integer with 1 to 4 fractional part (123.1234) and only 1 to 4 fractional part (.1234), that's usually accepted as a valid number. All options can be prefixed by plus or minus signs:
/^[-+]?(?:\d+|\d*(?:\.\d{1,4})?)$/
The (?:xxx) construction is preferred to (xxx) when we're not interested to capture the matches, because it requires less resources.

Categories

Resources