How to include dot once as last character in regex? - javascript

I'm trying to make regex in JavaScript that matches numbers between 1-100 and includes two decimals.
Example numbers that need to be included in the regex:
1
1.1
1.15
0.5
100
100.00
Example numbers that need to be excluded:
101
100.01
100.1
55.999
This is my regex at the moment:
^(?:100(?:\.00?)?|\d?\d(?:\.\d\d?)?)$
This works otherwise but I also need to include two numbers followed by a decimal or 100 followed by a decimal like this:
14.
9.
100.
This is because I'm running the regex check each time a button is pressed in an input field and even though number like 0.5 is allowed by the current regex, I can't type it in because 0. is not allowed.

You will need to use two separate regexps here, one for the live input validation (just what you described in the question, that will let you input allowed values), and another one for a final "on-submit" validation (that will check the validity of the whole input string).
Otherwise, you won't be able to input 0.5 like values.
The regex for live input validation is:
/^(?:100(?:\.0?0?)?|\d\d?(?:\.\d?\d?)?)$/
See this regex demo. Note how the ? quantifiers make patterns optional, especially the \d and 0 after \. patterns.
The regex for final validation of numbers starting with 0.01 to 100 is
/^(?!0*(?:\.0*)?$)(?:100(?:\.00?)?|\d?\d?(?:\.\d\d?)?)$/
See this regex demo.

Make the numbers after the dot optional, too.
^(?:100(?:\.00?)?|\d?\d(?:\.\d{0,2})?)$
If you want to disallow 0 before the dot if the numbers after are all zero or nothing, probably include a separate negative lookahead for that:
^(?:100(?:\.00?)?|(?!0\.0*$)\d?\d(?:\.\d{0,2})?)$
Nothing here or in your original regex should disallow 0.5; perhaps add more debugging details if that is genuinely something you are grappling with.

You may use this regex for your task:
^(?:0?\.(?:\d?[1-9]|[1-9]\d)|[1-9]\d?(?:\.\d{1,2})?|100(?:\.0{1,2})?)$
RegEx Demo

With your shown samples, could you please try following.
^(?:100(?:\.0{1,2})?|(?:(?:\d\d?)(?:\.\d{1,2})?))$
Online demo for above regex
Explanation: Adding detailed explanation for above.
^ ##Matching from starting of value here.
(?: ##Starting 1st capturing group here.
100(?:\.0{1,2})?| ##matching 100 with or without 1 to 2 zeroes OR
(?: ##Starting 2nd capturing group here.
(?:\d\d?) ##In a non-capturing group matching 0 to 9 and 0 to 9 optional.
(?:\.\d{1,2})? ##In a non-capturing group matching dot followed by 1 or 2 digits
) ##Closing 2nd capturing group here.
)$ ##Closing 1st capturing group at the end of value.

Related

Conditional regex javascript

I'm trying to make a regex which can match a phone number and so tell me if the phone number is either valid or not.
I would like to verify if there is a country id before the phone number, this one is well formated.
For example : +(33)0123456789 I want to be sure if the user start to type the first parenthesis it must be followed by number and ended by a closing parenthesis.
I have succeeded with PCRE engine
^[+]?((\()?(?(2)[0-9]{1,4})(?(2)\)))?([0-9]{2}){1}([\.\- ]?[0-9]{2}){4}$
But I realized this way doesn't work with javascript engine, conditional is not supported.
^[+]?((\()?((?=\2)[0-9]{1,4})((?=\2)\)))?([0-9]{2}){1}([\.\- ]?[0-9]{2}){4}$
It doesn't fill my needs. I want to check if the first parenthesis is set then it must be followed by number and a closing parenthesis.
So I ask you if there is a workaround in javascript to do this ?
Some help would be really appreciated, thank you :)
The ((\()?(?(2)[0-9]{1,4})(?(2)\)))? part of the regex is matching an optional sequence of smaller patterns. (\()? matches an optional ( and places it in Group 2. Then, (?(2)[0-9]{1,4}) matches 1 to 4 digits if Group 2 matched. Then (?(2)\)) matches ) if Group 2 matched. Basically, this is equal to (?:\([0-9]{1,4})\))?.
Thus, you need no conditional construct here.
You may use
^\+?(?:\([0-9]{1,4})\)?[0-9]{2}(?:[. -]?[0-9]{2}){4}$
See the regex demo
Details
^ - start of string
\+? - an optional +
(?:\([0-9]{1,4})\)? - an optional sequence: (, 1 to 4 digits and )
[0-9]{2} - 2 digits
(?:[. -]?[0-9]{2}){4} - 4 occurrences of an optional space, dot or - followed with 2 digits
$ - end of string.

Regex expression for numbers with commas and optional .00

I want to make a regex pattern for number input. When user enter numbers the comma is automatically separate entered number like 1,424 or 23,232. This is working fine with regex pattern
/^[0-9.,]*$/
But the problem is that this pattern allowed dot(.) between numbers. I want to make regex expression like the input can allowed .00 ate the end of numbers not between numbers. But the .00 is also optional.
Allowed number formats are below:
123312131256457.00
1233121312564
9,223,372
Not allowed number formats are below:
34.343455.3434
34353...
I spend lost of time on same but does't get any solution. Please share yours ideas. Thanks in advance.
Try this regex:
/^[0-9]{1,3}((,[0-9]{3})*|([0-9])*)(\.[0]{2})?$/
Here is a brief explanation:
^ from the start
[0-9]{1,3} match 1 to 3 numbers
(,[0-9]{3})* followed by a comma and three numbers, any number of times
([0-9])* OR just followed any amount of numbers, with NO commas
(\.[0-9]{2})? followed by an optional decimal point and two zeroes
$ end
Demo here:
Regex101
The very, very simple answer would be /^[0-9,]*(\.00)?$/. I.e., add an optional .00 suffix, remove support for . literal before the optional part.
As commented above you could go for something more fancy: ^(0|[1-9]\d{0,2}(,?\d{3})*)(\.00)?$
This will behave as commented:
0 // OK
01 // Not OK, must start with 1-9 if not 0 or 0.00
1,1,1.00 // Not OK, groups must be 3 digits, if used
0,000.00 // Not OK, should be 0.00
1,000 // OK
123312131256457.00 // OK, groups are optional
1233121312564 // OK, decimals (.00) are optional
9,223,372 // OK
just something like
/^[0-9,]*(?:\.0+)?$/
but [0-9,]* also allows "3,,,"

Regular Expression with optional elements in input string in javascript

Can anyone give me the regular expression for currency which have the following formats :
1000 - valid
1,000 - valid
1,000.00 or 1000.00 - valid.
This means, the number May or May Not contain a comma(,) separator every 3 digits.
The number May Or May Not contain a dot (.), and if it carries a dot(.) it should show atleast 1 number after the decimal place.
And lastly it should be numerical characters only. If I need to make my question clear kindly suggest.
/^\d{1,3}(?:(?:,\d{3})*|(?:\d{3})*)(?:\.\d{1,2})?$/
"Between one and three digits, then any number of groups of three digits prefixed by a comma or any number of groups of three digits not prefixed by said comma (disallowing a mix of the two kinds of groups), then an optional group of one or two digits prefixed by a dot."
Note: This regex assumes that you want to validate an entire string against the criteria outlined in your question. If you want to use it to find such numbers in a longer string, you will need to remove the ^ and $ from the beginning and end of the expression.
Something like so should work: (,?\d{3})+(\.\d{2})?. The regex will attempt to match a sequence of 3 digits precedeed by an optional comma, which is then, finally followed by an optional decimal point and 2 digits.
Please refer to this tutorial for more information.
EDIT: As per the comment below, the above regex can fail. I would recommend first using this regular expression: ^[\d][\d.,]+$ to make sure that you only have digits, thousand and decimal seperators. This regular expression will also make sure that the number starts with a digit, not with anything else. You could most likely have one regular expression which does everything, but it will most likely be quite complex.

Regex to validate brazilian money using Javascript

I need to validate some form fileds that contain brazilian money (its name is "Real") using Javascript. It has the following format:
0,01
0,12
1,23
12,34
123,45
1.234,56
12.235,67
123.456,78
1.234.567,89
12.345.678,90
123.456.789,01
1.234.567.890,12
My regex knowledge is weak, can somebody help me please?
Does this do what you want?
^\d{1,3}(?:\.\d{3})*,\d{2}$
That says "1 to 3 digits, optionally followed by any number of groups of three digits preceded by a period, followed by a comma and two more digits." If you want to allow the leading whitespace present in your example, add \s* to the front:
^\s*\d{1,3}(?:\.\d{3})*,\d{2}$
EDIT: As #ElRonnoco pointed out, the above regular expression accepts leading zeroes (e.g. 010.100,00). To disallow those, you may use this longer version:
^\s*(?:[1-9]\d{0,2}(?:\.\d{3})*|0),\d{2}$
EDIT 2 The above regular expressions all match a string containing a single monetary amount and nothing else. It's not clear from the question if that's the intent.
EDIT 3 To allow numbers that have no decimal part, or only one decimal digit, change it like this:
^\s*(?:[1-9]\d{0,2}(?:\.\d{3})*|0)(?:,\d{1,2})?$
I would give this regex a try:
\d+(?:\.\d{3})*?,\d{2}
What it says is:
- match digits until
a. a dot followed by 3 digits is found (and this step can be repeated several times)
b. or a comma followed by 2 digits is found
EDIT:
- thanks for the comments, I forgot about the constraint for the first value
updated regex
\d{1,3}(?:\.\d{3})*?,\d{2}
Complementing Mark's reply:
Who needs "." in the string and not "," to count cents. And need find the values in middle a text :
(?:[1-9]\d{0,2}(?:\,\d{3})*|0)(?:.\d{1,2})?
https://regexr.com/61166

Regular expression for number with length of 4, 5 or 6

I need a regular expression that validate for a number with length 4, 5, 6
I used ^[0-9]{4} to validate for a number of 4, but I do not know how to include validation for 5 and 6.
Try this:
^[0-9]{4,6}$
{4,6} = between 4 and 6 characters, inclusive.
[0-9]{4,6} can be shortened to \d{4,6}
Be aware that, as written, Peter's solution will "accept" 0000. If you want to validate numbers between 1000 and 999999, then that is another problem :-)
^[1-9][0-9]{3,5}$
for example will block inserting 0 at the beginning of the string.
If you want to accept 0 padding, but only up to a lengh of 6, so that 001000 is valid, then it becomes more complex. If we use look-ahead then we can write something like
^(?=[0-9]{4,6}$)0*[1-9][0-9]{3,}$
This first checks if the string is long 4-6 (?=[0-9]{4,6}$), then skips the 0s 0*and search for a non-zero [1-9] followed by at least 3 digits [0-9]{3,}.
If the language you use accepts {}, you can use [0-9]{4,6}.
If not, you'll have to use [0-9][0-9][0-9][0-9][0-9]?[0-9]?.
To match standalone 4-6-digit numbers, you may use
^\d{4,6}$ // If full string match is expected
\b\d{4,6}\b // If no letters/digits/underscores are expected on both ends
(?<!\d)\d{4,6}(?!\d) // If no digits are expected on both ends, but letters/_ are allowed
(^|\D)(\d{4,6})(?!\d) // Same as above, in case lookbehinds are not supported (get Group 2 value)
See Regex #1 - Regex #2 - Regex #3 and Regex #4 demos.
Details:
^ - start of string
\b - a word boundary
(?<!\d) - a negative lookbehind that fails the match if there is a digit immediately to the left of the current location
(^|\D) - a capturing group matching either start of string or a non-digit char
\d{4,6} - four, five or six digits
(?!\d) - a negative lookahead that fails the match if there is a digit immediately to the right of the current location
$ - end of string

Categories

Resources