Regular Expression For Decimal with precision 5 and scale 2 - javascript

I am trying to validate a text box on key press and here is my criteria.
I need a regular expression which allows total of 5 digits and a decimal point.
If there's no decimal point then it should allow to enter 5 digits.
And if there's a decimal point then it should allow only 2 digits after the decimal point.
However the total number of digits should not exceed 5 digits excluding decimal point and it can be less than 5 digits.
And below is my regex
/^(?:\d{1,2}(?:\.\d{0,6})?)?$/
I hope I am clear and let me know if any clarifications needed
Please help Thanks in advance.

Here's a regex that just lists out alternatives separated by |:
/^\d{1,5}|\d{1,4}\.\d|\d{1,3}\.\d{2}$/
i.e.
1-5 digits with no decimal point OR 1-4 digits followed by a decimal point and one digit after the decimal OR 1-3 digits followed by a decimal point and two digits after the decimal.

Related

Javascript length limit of regex with given decimal conditions

I could make Javacsript regex with 2 decimal points with 3 integer but the thing is I have to make it 6 digits total including the decimal "dot".
So this result should be a minimum 0 to 999.99 with the condition it's a minimum 0 to 6 digit length.
Below is my solution So far:
^(\d{0}(?:\d{1,3})|\d{1}\.(?:\d{1,2})|\d{2}\.(?:\d{1,2})|\d{3}\.(?:\d{1,2}))$
Explanation:
If it's 0 digits, then min 1 to 3 digits so I can make it max 999.
If it's 1 digit w/ decimal then max 2 decimal points (ex) 2.22
If it's 3 digits w/ decimal then max 2 decimal points. (ex) 3.33
This is for regex in my JSP input (which is text type) and I'm literally suffering for this problem for days.
(this regex is for versions to be specific..)
Any help or better alternative way would help me A LOT.
google search, try by myself, online course, Youtube
If I get it well: you need to match a decimal with a length of 6 including the decimal point.
The following can do:
^(\d{1,3}(?:\.\d{1,2})?)$
It specified a integer part up to 3 digits, then an optional decimal part.
It has some edge cases of course such as matching: 000.00 which can be cleaned up further if needed depending on your expected input.
Sample: https://regex101.com/r/LrOHvt/1

angularjs ng-pattern regex for 4 digits and 2 decimal

I am trying to create a Regex for a number with maximum 4 digits and if the input has decimal it has to have 2 digits - .20 and not .1.
tried:
ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" //fail for 666666, .10, .1
Examples for valid inputs:
100.10
100
3000.10
Example for invalid:
10000 //has more then 4 digits before decimal
100.1 //has only 1 digit after decimal
.10 //has no digits before decimal
Thanks for any help.
Use {#,#} to limit the number of digits to 1 to 4
Try
^[0-9]{1,4}(\.[0-9][0-9])?$
Use ( )? to make an optional two-digit decimal part
The problem with using the {1,2} is that it allows one or two digits, when you really only want two. And I assume you want to enforce a rule that if they have a ".", they must have two digits?
For example
var patt = /^[0-9]{1,4}(\.[0-9][0-9])?$/i
"1011.11".match(patt)!==null
"1011.1".match(patt)!==null
Returns
true
false
With gratitude to Sebastian Proske and Wiktor Stribiżew
For pointing out the need to escape the .

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 to check amount is greator then one

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.

Regex for accepting decimal numbers in javascript

I want regex which will accept decimal numbers The decimal number like '12.12,123.23,09.90 or 2.78' i.e there should be 2 decimal places after decimal point and should accept 1,2 or 3 digits before decimal point i.e its optional for 1,2 or 3 digits before decimal point.
I've added this but not works
var validates=/^\d+\.\d{0,3}$/;
You can use this regex:
var validates = /^\d{0,3}\.\d{1,2}$/m;
RegEx Demo

Categories