Inputmask - Regex currency and optional decimal places - regex

I'm trying to create a currency Regex using the Robin Herbots Inputmask plugin and can't find a way to make it all work together
I need a regex that allows precision from 2 to 10 digits and adds a comma on the thousands, like these:
1,123.00
123.12345
1,123,456.1234567890
and so on. I ended up with these regexes ^\\d{1,3}(?:,\\d{3})*$ and \d{1,99},\d{2,10}. They both achieve what I need, but separately. How to I make them both work together?
Thanks!

You can use
/^(?=(?:,?\d){1,99}(?:\.|$))\d{1,3}(?:,\d{3})*(?:\.\d{2,10})?$/
See the regex demo.
Details:
^ - start of string
(?=(?:,?\d){1,99}(?:\.|$)) - a positive lookahead that requires 1 to 99 occurrences of an optional , followed with a digit, and then followed with either . or end of string
\d{1,3} - one to three digits
(?:,\d{3})* - zero or more occurrences of a comma and then three digits
(?:\.\d{2,10})? - an optional occurrence of a . and two to ten digits
$ - end of string.

Related

Regex for a promo code that has the following rules

I need to build a regex that have the following:
Rules to be applied:
exactly 14 characters
only letters (latin characters) and numbers
at least 3 letters
Regex still confuses me so I am struggling to get the correct output. I want to use it with swift and swiftui in an app I am making
(?=(.*[a-zA-Z]){3,}([0-9]){0,}){14,14}$
I tried this. But I know it is not the way
I would use a positive lookahead for the length requirement:
^(?=.{14}$)(?:[A-Za-z0-9]*[A-Za-z]){3}[A-Za-z0-9]*$
This pattern says to match:
^ from the start of the input
(?=.{14}$) assert exact length of 14
(?:
[A-Za-z0-9]*[A-Za-z] zero or more alphanumeric followed by one alpha
)
[A-Za-z0-9]* any alphanumeric zero or more times
$ end of the input
You need to use
^(?=(?:[0-9]*[a-zA-Z]){3})[a-zA-Z0-9]{14}$
Details
^ - start of string
(?=(?:[0-9]*[a-zA-Z]){3}) - at least three repeations of a letter after any zero or more digits sequence required
[a-zA-Z0-9]{14} - fourteen letters/digits
$ - end of string.
See the regex demo.

Regex to block more than 3 numbers in a string

I am trying to block any strings that contain more than 3 numbers and prevent special characters. I have the special characters part down. I'm just missing the number part.
For example:
"Hello 1234" - Not Allowed
"Hello 123" - Allowed
I've tried the following:
/^[!?., A-Za-z0-9]+$/
/((^[!?., A-Za-z]\d)([0-9]{3}+$))/
/^((\d){2}[a-zA-Z0-9,.!? ])*$/
The last one is the closest I got as it prevents any special characters and any numbers from being entered at all.
I've looked through previous posts, but am coming up short.
Edit for clarification
Essentially I'm trying to find a way to prevent customers from entering PII on a form. No submission should be allowed that contains more than 3 numbers in a string.
Hello1234 - Not allowed
12345 - Not allowed
1111 - not allowed
No where in the comment section when the user enters the string should there be more than 3 numbers in total.
About the patterns that you tried
^[!?., A-Za-z0-9]+$ The pattern matches 1+ times any of the listed, including 1 or more digits
((^[!?., A-Za-z]\d)([0-9]{3}+$)) If {3}+ is supported, the pattern matches a single char from the character class, 1 digit followed by 3 digits
^((\d){2}[a-zA-Z0-9,.!? ])*$ The pattern repeats 0+ times matching 2 digits and 1 of the listed in the character class
You can use a negative lookahead if that is supported to assert not 4 digits in a row.
^(?!.*\d{4})[a-zA-Z0-9,.!? ]+$
regex demo
If there can not be 4 digits in total, but 0-3 occurrences:
^[a-zA-Z,.!? ]*(?:\d[a-zA-Z,.!? ]*){0,3}$
Explanation
^ Start of string
[a-zA-Z,.!? ]* Match 0+ times any of the listed (without a digit)
(?:\d[a-zA-Z,.!? ]*){0,3} Repeat 0 - 3 times matching a single digit followed by optional listed chars (Again without a digit)
$ End of string
regex demo
If you don't want to match an empty string and a lookahead is supported:
^(?!$)[a-zA-Z,.!? ]*(?:\d[a-zA-Z,.!? ]*){0,3}$
See another regex demo
Here is my two cents:
^(?!(.*\d){4})[A-Za-z ,.!?\d]+$
See the online demo
^ - Start string anchor.
(?! - Open a negative lookahead.
( - Open capture group.
.*\d - Match anything other than newline up to a digit.
){4} - Close capture group and match it 4 times.
) - Close negative lookahead.
[A-Za-z ,.!?\d]+ - 1+ Characters from specified class.
$ - End string anchor.
I think it should cover what you described.
Assuming you mean <= 3 digits, this may be a naive one but how about
[ALLOWED_CHARS]*[0-9]?[ALLOWED_CHARS]*[0-9]?[ALLOWED_CHARS]*[0-9][ALLOWED_CHARS]*?
Fill [ALLOWED_CHARS] to whatever you define is not special character and nums.

Regex how to identify length of numbers

lets say i have String which starts with two letters and letters are followed with underscore and numbers
XX_333-335 [A-Za-z]{2}_\d{3}-\d{3}`
` - this works good for me
but string can be: (this works as well)
XX_333-335;338;340-341 -^[A-Za-z]{2}_\d{3}-\d{3};|\d{3}|\d{3}-\d{3}
but how to check with regex if number length is 3 places?
XX_0333-0335;0338;340-0341 - In this example there should be no matches, because numbers have four positions not three
Is there any way how to solve it with regex?
thank you
You can use
^[A-Za-z]{2}_\d{3}(?:-\d{3})?(?:;\s*\d{3}(?:-\d{3})?)*$
See the regex demo
Details
^ - start of string
[A-Za-z]{2} - two ASCII letters
_ - an underscore
\d{3} - three digits
(?:-\d{3})? - an optional occurrence of - and three digits
(?:;\s*\d{3}(?:-\d{3})?)* - zero or more occurrences of a semi-colon followed with 0+ whitespaces, three digits and an optional sequence of three digits
$ - end of string.
This would do it:
^[A-Za-z]{2}_(?:\d{3}(?:[; -]+|$))+$
https://regex101.com/r/4ULAql/1

Validating User Input While Typing using RegEx

I am struggling to write the RegEx for the following criteria:
The number can be positive / negative
Optional - at the start
Between 1 and 5 numbers before the decimal point
2 decimal places only (optional)
Stop user from typing more than 1 . or -
This is the regex I have tried to implement which does not work for me.
^((-?[0-9]{1,5}(\.?){1,1}[0-9]{0,2})
It should allow the user to type out the following numbers.
-1.12
12345
1
123
12.12
Any help would be appreciated!
You may use
^-?\d{0,5}(?:(?<=\d)\.\d{0,2})?$
See the regex demo.
Details
^ - start of string
-? - an optional -
\d{0,5} - zero to five digits
(?:(?<=\d)\.\d{0,2})? - an optional sequence of
(?<=\d) - there must be a digit immediately to the left of the current location
\. - a dot
\d{0,2} - zero, one or two digits
$ - end of string.
If you want to validate while typing, you could make use of optional groups to accept intermediate values and do a final check on the whole pattern when processing the value.
^-?(?:\d{1,5}(?:\.\d{0,2})?)?$
Explanation
^ Start of string
-? Optional hyphen
(?: Non capture group
\d{1,5} Match 1-45 digits
(?: Non capture group
\.\d{0,2} Match a dot and 0-2 digits
)? Close group and make it optional
)? Close group and make it optional
$ End of string
Regex demo
To validate the final pattern, you could match an optional -, 1-5 digits and an optional decimal part:
^-?\d{1,5}(?:\.\d{1,2})?$
Regex demo
The regex ^(-?(\d{1,5}(\.\d{0,2})?)?)$ should work if you want to match strings that end in . such as 123. demo of this regex
Otherwise, change the 0 to a 1 as follows: ^(-?(\d{1,5}(\.\d{1,2})?)?)$. Then it will only match strings that have a digit after the decimal point.
The regex that you posted allows strings with more than 2 digits after the decimal point because it stops matching after the 2 digits, even if the string continues. Adding a $ at the end of the regex stops it from matching strings that continue after the part we want.
This regex ^(-?\d{1,5}(\.\d{0,2})?)$ will validate the input once the user has finished typing, because I assume that you don't want -to be valid at that point.

Only allow 2 digits in a string using regex

I need regex that only allows a maximum of 2 digits (or whatever the desired limit is actually) to be entered into an input field.
The requirements for the field are as follows:
Allow a-z A-Z
Allow 0-9
Allow - and . characters
Allow spaces (\s)
Do not allow more than 2 digits
Do not allow any other special characters
I have managed to put together the following regex based on several answers on SO:
^(?:([a-zA-z\d\s\.\-])(?!([a-zA-Z]*\d.*){3}))*$
The above regex is really close. It works successfully for the following:
test 12 test
test12
test-test.12
But it allows an input of:
123 (but not 1234, so it's close).
It only needs to allow an input of 12 when only digits are entered into the field.
I would like some help in finding a more efficient and cleaner (if possible) solution than my current regex - but it must still be regex, no JS.
You could use a positive lookahead like
(?=^(?:\D*\d\D*){2}$) # only two digits
^[- .\w]+$ # allowed characters
See a demo on regex101.com.
You may use a negative lookahead anchored at the start that will make the match fail once there are 3 digits found anywhere in the string:
^(?!(?:[^0-9]*[0-9]){3})[a-zA-Z0-9\s.-]*$
^^^^^^^^^^^^^^^^^^^^^^^
See the regex demo
Details:
^ - start of string
(?!(?:[^0-9]*[0-9]){3}) - the negative lookahead failing the match if exactly 3 following sequences are found:
[^0-9]* - zero or more chars other than digits
[0-9] - a digit (thus, the digits do not have to be adjoining)
[a-zA-Z0-9\s.-]* - 0+ ASCII letters, digits, whitespace, . or - symbols
$ - end of string.