Regex For digit count in an alphanumeric string - javascript

How can i validate following logic from regex
Allow Dollar($),Comma(,),Decimal(.) and digit(0-9)
Can have only 8 digit before Decimal irrespective of $ and comma
2 digit after decimal
Allowed string Example:
99999999.99
$99999999.99
$9,999,9999.99
$9999,99,99.99
Does not allow :
- $999999999.99 ( 9 digit before decimal)
- $99,99,999,99.99
Mean i want to restrict the count of digit only before decimal.
How can i achieve this. Thanks in Advance

You can use this regex:
/^\$?(?:,?\d){1,8}(?:\.\d{1,2})?$/gm
RegEx Demo
Explanation:
^ # Line start
\$? # match optional $ at start
(?:,?\d) # Match an optional comma followed by a digit and use non-capturing group
{1,8} # up to 8 occurrence of previous group
(?:\.\d{1,2})? # followed by optional decimal point and 1 or 2 digits
$ # line end

/^(\$?(\,?\d){1,8}\.\d{2}$)/gm
Regex Demo

Related

Regex validation

Minimum 3 characters, all small case, can use maximum of 2 numbers, no special characters allowed.
I tried using ^[a-zA-Z0-9]*$ but I'm unable to limit the numbers used
Can someone help me.
You could use a negative lookahead assertion to rule out more than 3 digits:
/^(?!(?:.*\d+){3,})[a-z0-9]{3,}$/
Here is an explanation of the pattern:
^ from the start of the string
(?!(?:.*\d+){3,}) assert that 3 or more digits do NOT occur
[a-z0-9]{3,} then match 3 or more lowercase letters or digits
$ end of the string
Here is a working demo.
You could use check if there are at least 3 allowed characters, and then match 0, 1 or 2 digits.
^(?=[A-Za-z\d]{3})[A-Za-z]*(?:\d[A-Za-z]*){0,2}$
Explanation
^ Start of string
(?=[A-Za-z\d]{3}) Positive lookahead, assert 3 allowed chars
[A-Za-z]* Match optional chars A-Za-z
(?:\d[A-Za-z]*){0,2} Repeat 0-2 times matching a single digit and optional chars A-Za-z
$ End of string
See the matches on regex101.

Regex - Allow one or two alphabet and its can be at anywhere in string

I want regex which can fulfill below requirement:
Between 6 and 10 total characters
At least 1 but not more than 2 of the characters need to be alpha
The alpha characters can be anywhere in the string
We have tried this but not working as expected : (^[A-Z]{1,2}[0-9]{5,8}$)|(^[A-Z]{1}[0-9]{4,8}[A-Z]{1}$)|(^[0-9]{4,8}[A-Z]{1,2}$)|([^A-Z]{3}[0-9]{6,9})
Can anyone please help me to figure it out?
Thanks
You can assert the length of the string to be 6-10 char.
Then match at least a single char [A-Z] between optional digits, and optionally match a second char [A-Z] between optional digits.
^(?=[A-Z\d]{6,10}$)\d*[A-Z](?:\d*[A-Z])?\d*$
^ Start of string
(?=[A-Z\d]{6,10}$) Positive lookahead to assert 6-10 occurrences of A-Z or a digit
\d*[A-Z] Match optional digits and then match the first [A-Z]
(?:\d*[A-Z])? Optionally match optional digits and the second [A-Z]
\d* Match optional digits
$ End of string
See a regex demo.
One option is to use the following regular expression:
^(?=.*[a-z])(?!(?:.*[a-z]){3})[a-z\d]{6,10}$
with the case-indifferent flag i set.
Demo
This expression reads, "Match the beginning of the string, assert the string contains at least one letter, assert the string does not contain three letters and assert the string contains 6-10 characters, all being letters or numbers".
The various parts of the expression have the following functions.
^ # match the beginning of the string
(?= # begin a positive lookahead
.*[a-z] # match zero or more characters and then a letter
) # end positive lookahead
(?! # begin a negative lookahead
(?: # begin a non-capture group
.*[a-z] # match zero or more characters and then a letter
){3} # end non-capture group and execute it 3 times
) # end negative lookahead
[a-z\d]{6,10} # match 6-10 letters or digits
$ # match end of string
Note that neither of the lookaheads advances the string pointer maintained by the regex engine from the beginning of the string.

regex to only allow an input to 3 decimal places with 0.001 being the smallest number possible, not 0

I am trying to write a regex to allow a user enter a positive number and to 3 decimal places. My regex looks like this, however, it isn't working as I would like.
/\d*[1-9](\.\d{0,3})?/
This allows the user to enter 1.000 as the smallest number, however, it doesn't allow a user to enter 0.001 which should be the smallest number possible to enter into the input.
Does anyone know what the regex should be to solve this?
Your code has another issue where it can not match 10 since you are not allowing the ones place to be 0.
You need to use some or statements
const re = /(^([1-9]|\d{2,})(\.\d{0,3})?|0\.\d{0,2}[1-9])$/
const tests = ["0.001", "0.1", "0","0.0", "0.000","10.001", "10","11","1"]
tests.forEach(n => console.log(n, re.test(n)))
const re = /^(?!0+(?:\.0+)?$)\d+(?:\.\d+)?$/
const tests = ["0.001", "0.1", "0","0.0", "0.000","10.001", "10","11","1","1.22","1.222"]
tests.forEach(n => console.log(n, re.test(n)))
Explanation:
^ # beginning of string
(?! # negative lookahead, make sure we haven't after:
0+ # 1 or more zero
(?: # start non capture group
\. # a dot
0+ # 1 or more zero
)? # end group, optional
$ # end of string
) # end lookahead
\d+ # 1 or more digits
(?: # start non capture group
\. # a dot
\d+ # 1 or more digits
)? # end group, optionnal
$ # end of string
Personally I would just check for 0 and make the regex a lot simpler, but here is a solution, where the required decimal places can be adjusted by changing {1,3}.
The jist of this regex is that we allow any number greater than two digits , then allow only 1-9 for one digit, then optionally require up to 1 decimal with 1-3 digits afterwards.
const r = /^((([0-9]{2,}){1}|[1-9]{1})(\.[0-9]{1,3}){0,1})$/;
const tests = ['1','2','0','1.001','1.001.1','999.001','9.01','9.0100','abc'];
tests.forEach(t=>console.log(t,r.test(t)));
Another option is to use a negative lookahead to assert from the start of the string what is on the right is neither a dot or zero repeated until the end of the string:
^(?![0.]+$)\d+(?:\.\d{1,3})?$
See a Regex demo
Explanation
^ Start of the string
(?![0.]+$) Negative lookahead to assert what is on the right is not what is listed in the character class repeated 1+ times until the end of the string
\d+ Match 1+ times a digit
(?:\.\d{1,3})? Optional non capturing group which matches a dot and 1+ times a digit
$ End of the string
const tests = ["0.001", "0.1", "0","0.0", "0.000","10.001", "10","11","1","1.22","1.222"]
tests.forEach(n => console.log(parseFloat(n) >= 0.001))
I really think this is being overthought.
The answer is here.
([1-9]\.[0-9][0-9][0-9]|[0]\.[1-9][0-9][0-9]|[0]\.[0][1-9][0-9]|[0]\.[0][0][1-9])
This should match 0.001~9.999

Regex validation for mixed digits for a max of 6 characters

I need a regex validation for mixed length, a total length of 6 characters in that 4-6 characters in caps/numbers and 0-2 spaces.
I tried like
^[A-Z0-9]{4,6}+[\s]{0,2}$
but it results in a max length of 8 characters, but I need a max of 6 characters.
If the alphanumeric chars should only appear at the start of the string and the whitespaces can appear at the end (i.e. the order of the alphanumerics and whitespaces matters), you may use
/^(?=.{6}$)[A-Z0-9]{4,6}\s*$/
See the regex demo
Details
^ - start of string
(?=.{6}$) - the string length is restricted to exactly 6 non-line break chars
[A-Z0-9]{4,6} - 4, 5 or 6 uppercase ASCII letters or digits
\s* - 0+ whitespaces (but actually, only 0, 1 or 2 will be possible to add as the total length is already validated with the lookahead)
$ - end of string.
If you want to match the alphanumeric and whitespaces anywhere inside the string, you need a lookaround based regex like
^(?=(?:[^A-Z0-9]*[A-Z0-9]){4,6}[^A-Z0-9]*$)(?=(?:\S*\s){0,2}\S*$)[A-Z0-9\s]{6}$
See the regex demo
Details
^ - start of string
(?=(?:[^A-Z0-9]*[A-Z0-9]){4,6}[^A-Z0-9]*$) - a positive lookahead that requires the presence of 4 to 6 letters or digits anywhere inside the string
(?=(?:\S*\s){0,2}\S*$) - a positive lookahead that requires the presence of 0 to 2 whitespaces anywhere inside the string
[A-Z0-9\s]{6} - 6 ASCII uppercase letters, digits or whitespaces
$ - end of string.
To shorten the pattern, the second lookahead can be written as (?!(?:\S*\s){3}), it will fail the match if there are 3 whitespace chars anywhere inside the string. See the regex demo.
You can use | characters to accommodate several cases into one.
const regex = /(^[A-Z0-9]{4}\s{2}$)|(^[A-Z0-9]{5}\s$)|(^[A-Z0-9]{6}$)/g;
alert(regex.test(prompt('Enter input, including space(s)')));
If you want to match zero, one or two spaces at the end, you could use an alternation for those 3 cases.
^(?:[A-Z0-9]{4}[ ]{2}|[A-Z0-9]{5}[ ]|[A-Z0-9]{6})$
Regex demo
Explanation
^ Assert the start of the string
(?: Non capturing group
[A-Z0-9]{4}[ ]{2} Match uppercase or digit 4 times followed by 2 spaces
| Or
[A-Z0-9]{5} Match uppercase or digit 5 times followed by 1 space
| Or
[A-Z0-9]{6} Match uppercase or digit 6 times
) Close non capturing group
$ Assert the end of the string

Regular Expression to also allow slash "/" with existing Regex

I am facing difficulty to allow slash "/" with an existing regex
Below is an existing Regex which allows dot and numbers:
val.match(/^[0-9]+(\.[0-9]{1,2})?$/)
I changes it to...
val.match(/^[0-9]+([./][0-9\/]{1,2})?$/)
But this one won't allow the number like 1.5/384 where both dot/period and slash simultaneously.
Can someone help me with it?
You may add an optional non-capturing group after your main pattern part to match 1 or 0 occurrences of / followed with 1 or more digits:
/^\d+(?:\.\d{1,2})?(?:\/\d+)?$/
^^^^^^^^^^
See the regex demo
Details
^ - start of string
\d+ - 1 or more digits
(?:\.\d{1,2})? - an optional sequence of . and then 1 or 2 digits
(?:\/\d+)? - an optional sequence of / and then 1+ digits
$ - end of string.
If the number after / can be float in the same format as the first number:
/^\d+(?:\.\d{1,2})?(?:\/\d+(?:\.\d{1,2})?)?$/
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
This should do what you want :
^(\d+(?:\.\d{1,2})?\/?(?:\d+\.\d{1,2})?)$
See this Regex101.com
Edit : Corrected the fact that it didn't match 1 or 1.5

Categories