I am developing an application with MVC 4, razor. For one change Password view I need to do some validation which has following conditions:
It should match at least 3 of the following:
1. Upper case alphabetic characters
2. Lower case alphabetic characters
3. Numbers
4. Special keyboard characters (except script tags)
Now, I have written following regular expression in my model
[RegularExpression("^([a-zA-Z0-9#$%=#!{},`~&*()'?.:;_|^/+\"-]{8,32})$", ErrorMessage = "Current Password is invalid")]
but, this will match any of the given subsets. I need to match atleast 3 of them. Can anybody please help me how do I do that?
It might not be a good idea to use regex for password validation, but using lookaheads and the or operator | allows you to do this:
^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])|(?=.*[#$%=#!{},`~&*()'?.:;_|^/+\"-])(?=.*[a-z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[#$%=#!{},`~&*()'?.:;_|^/+\"-])(?=.*[0-9])|(?=.*[A-Z])(?=.*[a-z])(?=.*[#$%=#!{},`~&*()'?.:;_|^/+\"-])).{8,32}$
There are 4 parts in this regex:
(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])
Makes sure there's at least 1 lower alpha, 1 upper alpha and 1 number.
(?=.*[#$%=#!{},`~&*()'?.:;_|^/+\"-])(?=.*[a-z])(?=.*[0-9])
Makes sure there's at least 1 special character, 1 upper alpha and 1 number.
(?=.*[A-Z])(?=.*[#$%=#!{},`~&*()'?.:;_|^/+\"-])(?=.*[0-9])
Makes sure there's at least 1 upper alpha, 1 special character and 1 number.
(?=.*[A-Z])(?=.*[a-z])(?=.*[#$%=#!{},`~&*()'?.:;_|^/+\"-])
Makes sure there's at least 1 upper alpha, 1 lower alpha and 1 special character.
And using | with those four (grouped into a non-capturing group) gives you the 3 out of 4 validation.
Then use .{8,32} for the length of the password
Related
I'm trying to create a regex that enforces:
whole numbers only, no decimals/fractions
thousands separated by commas
sets a maximum value allowed. Acceptable range of 1-25,000,000,000 (25 billion)
I created the following regex that already accomplishes the first 2 requirements, only allowing acceptable values like:
1
1,000
25,000
250,000,000 etc.
but it's the 3rd requirement of setting a maximum value of 25 billion that I'm struggling with.
Does anyone know a way to enhance this current pattern to only allow values between the range of 1 - 25,000,000,000 ?
^[1-9]\d?\d?$|^(?!0,)(?!0\d,)(?!0\d\d,)(\d\d?\d?,)+\d{3}$
I did a lot of searching, and I found a regex that could impose a maximum value, but I can't quite figure out how to modify it to what I need to meet all 3 requirements. This is the one I found:
^((25000000000)|(2[0-4][0-9]{9})|(1[0-9]{10})|([1-9][0-9]{9})|([1-9][0-9]{8})|([1-9][0-9]{7})|([1-9][0-9]{6})|([1-9][0-9]{5})|([1-9][0-9]{4})|([1-9][0-9]{3})|([1-9][0-9]{2})|([1-9][0-9]{1})|([1-9]))$
I think this should do the trick:
^([1-9]\d{0,2}(,\d{3}){0,2})$|^(([1-9]|1\d|2[1-4])(,\d{3}){3})$|^25(,000){3}$
This regex consist of 3 main blocks or conditions:
[1-9]\d{0,2}(,\d{3}){0,2}: Any 1-9 followed by up to 2 digits, followed by up to 2 optional blocks of 3 digits preceded with a comma (supports up to 999,999,999).
([1-9]|1\d|2[1-4])(,\d{3}){3}: Three possible billion values: 1-9, or a 1 followed by any digit (to support 10-19), or a 2 followed by a 1-4 digit (to support 20-24). Then followed by 3 blocks of comma and 3 digits (supports up to 24,999,999,999).
25(,000){3}: Finally, special case, support for 25,000,000,000.
It matches:
1
12
123
1,000
25,000
250,000
2,500,000
24,999,999
25,000,000
250,000,000
1,500,000,000
2,500,000,000
15,000,000,000
24,999,999,999
25,000,000,000
And does not match:
0
1234
0,000
0,000,999
0,999,999,999
25,000,000,001
99,999,999,999
250,000,000,000
25,000,000,000,000
99,99,999
9,9,9,9,999
24999999999
25000000000
25000000001
26000000000
35000000000
I want to check if a number is 50 or more using a regular expression. This in itself is no problem but the number field has another regex checking the format of the entered number.
The number will be in the continental format: 123.456,78 (a dot between groups of three digits and always a comma with 2 digits at the end)
Examples:
100.000,00
50.000,00
50,00
34,34
etc.
I want to capture numbers which are 50 or more. So from the four examples above the first three should be matched.
I've come up with this rather complicated one and am wondering if there is an easier way to do this.
^(\d{1,3}[.]|[5-9][0-9]|\d{3}|[.]\d{1,3})*[,]\d{2}$
EDIT
I want to match continental numbers here. The numbers have this format due to internal regulations and specify a price.
Example: 1000 EUR would be written as 1.000,00 EUR
50000 as 50.000,00 and so on.
It's a matter of taste, obviously, but using a negative lookahead gives a simple solution.
^(?!([1-4]?\d),)[1-9](\d{1,2})?(\.\d{3})*,\d{2}\b
In words: starting from a boundary ignore all numbers that start with 1 digit OR 2 digits (the first being a 1,2,3 or 4), followed by a comma.
Check on regex101.com
Try:
EDIT ^(.{3,}|[5-9]\d),\d{2}$
It checks if:
there 3 chars or more before the ,
there are 2 numbers before the , and the first is between 5 and 9
and then a , and 2 numbers
Donno if it answer your question as it'll return true for:
aa50,00
1sdf,54
But this assumes that your original string is a number in the format you expect (as it was not a requirement in your question).
EDIT 3
The regex below tests if the number is valid referring to the continental format and if it's equal or greater than 50. See tests here.
Regex: ^((([1-9]\d{0,2}\.)(\d{3}\.){0,}\d{3})|([1-9]\d{2})|([5-9]\d)),\d{2}$
Explanation (d is a number):
([1-9]\d{0,2}\.): either d., dd. or ddd. one time with the first d between 1 and 9.
(\d{3}\.){0,}: ddd. zero or x time
\d{3}: ddd 3 digit
These 3 parts combined match any numbers equals or greater than 1000 like: 1.000, 22.002 or 100.000.000.
([1-9]\d{2}): any number between 100 and 999.
([5-9]\d)): a number between 5 and 9 followed by a number. Matches anything between 50 and 99.
So it's either the one of the parts above or this one.
Then ,\d{2}$ matches the comma and the two last digits.
I have named all inner groups, for better understanding what part of number is matched by each group. After you understand how it works, change all ?P<..> to ?:.
This one is for any dec number in the continental format.
^(?P<common_int>(?P<int>(?P<int_start>[1-9]\d{1,2}|[1-9]\d|[1-9])(?P<int_end>\.\d{3})*|0)(?!,)|(?P<dec_int_having_frac>(?P<dec_int>(?P<dec_int_start>[1-9]\d{1,2}|[1-9]\d|[1-9])(?P<dec_int_end>\.\d{3})*,)|0,|,)(?=\d))(?P<frac_from_comma>(?<=,)(?P<frac>(?P<frac_start>\d{3}\.)*(?P<frac_end>\d{1,3})))?$
test
This one is for the same with the limit number>=50
^(?P<common_int>(?P<int>(?P<int_start>[1-9]\d{1,2}|[1-9]\d|[1-9])(?P<int_end>\.\d{3})+|(?P<int_short>[1-9]\d{2}|[5-9]\d))(?!,)|(?P<dec_int_having_frac>(?P<dec_int>(?P<dec_int_start>[1-9]\d{1,2}|[1-9]\d|[1-9])(?P<dec_int_end>\.\d{3})+,)|(?P<dec_short_int>[1-9]\d{2}|[5-9]\d),)(?=\d))(?P<frac_from_comma>(?<=,)(?P<frac>(?P<frac_start>\d{3}\.)*(?P<frac_end>\d{1,3})))?$
tests
If you always have the integer part under 999.999 and fractal part always 2 digits, it will be a bit more simple:
^(?P<dec_int_having_frac>(?P<dec_int>(?P<dec_int_start>[1-9]\d{1,2}|[1-9]\d|[1-9])(?P<dec_int_end>\.\d{3})?,)|(?P<dec_short_int>[1-9]\d{2}|[5-9]\d),)(?=\d)(?P<frac_from_comma>(?<=,)(?P<frac>(?P<frac_end>\d{1,2})))?$
test
If you can guarantee that the number is correctly formed -- that is, that the regex isn't expected to detect that 5,0.1 is invalid, then there are a limited number of passing cases:
ends with \d{3}
ends with [5-9]\d
contains \d{3},
contains [5-9]\d,
It's not actually necessary to do anything with \.
The easiest regex is to code for each of these individually:
(\d{3}$|[5-9]\d$|\d{3},|[5-9]\d)
You could make it more compact and efficient by merging some of the cases:
(\d{3}[$,]|[5-9]\d[$,])
If you need to also validate the format, you will need extra complexity. I would advise against attempting to do both in a single regex.
However unless you have a very good reason for having to do this with a regex, I recommend against it. Parse the string into an integer, and compare it with 50.
I need to check icd10 code this code generate with few condition
min length is 3.
first character is letter and not is 'U'.
second and third is digit.
fourth is dot(.)
fifth to eight charactor is letter or digit.
Ex.:
Right : "A18.32","A28.2","A04.0","A18.R252", "A18", "A18.52", "R18", "R18."
Wrong : "A184.32","U18","111."
is this an icd-10-cm code you are looking to verify.
if so I believe that the 3rd digit is alpha or numeric
taken from page 7
https://www.cms.gov/Medicare/Coding/ICD10/downloads/032310_ICD10_Slides.pdf
if so the following regular expression should validate.
^([a-tA-T]|[v-zV-Z])\d[a-zA-Z0-9](\.[a-zA-Z0-9]{1,4})?$
otherwise you can edit the above regular expression to check characte 2 and 3 as numeric.
^([a-tA-T]|[v-zV-Z])\d{2}(\.[a-zA-Z0-9]{1,4})?$
You could try something like so: ^[A-TV-Z]\d{2}(\.[A-Z\d]{0,4})?$. An example is available here.
This is how the answer satisfies your condition:
Min length is 3: ^[A-TV-Z]\d{2}...$ attempts to match a letter and 2 digits. The ^ and $ ensure that there is nothing else in the string which does not satisfy the regular expression. This segment: (\.[A-Z\d]{0,4})? is surrounded by the ? operator: (...)?. This means that the content within the round brackets may or may not be there.
First character is letter and not is 'U'. This is satisfied by [A-TV-Z], which matches all the upper case letters which are between A and T, V and Z inclusive. This omits the letter U.
Second and third is digit. \d{2} means match two digits.
Fourth is dot(.): This is satisfied by \.. The extra \ is needed because the period character is a special character in regular expressions, which means match any character (exception new lines, unless a special option is passed along).
Fifth to eight charactor is letter or digit. [A-Z\d]{0,4} means any letter or digits, repeated between 0 and 4 times.
Try this:
\b[a-tv-zA-TV-Z]\d{2}(\.[a-zA-Z0-9]{,4})?\b
I assume by your example the dot and everything after it is optional
This regex will match a word boundary \b, a letter other than u or U [a-tv-zA-TV-Z], two digits \d{2} and then an optional dot followed by 0-4 letters or digits (\.[a-zA-Z0-9]{,4})? and a second word boundary \b
This question is old, but I had the same issue of validating ICD-10 codes, so it seemed worth an updated answer.
As it turns out, there are two flavors of ICD-10 codes: ICD-10-CM and ICD-10-PCS. From their usage guidelines:
The ICD-10-CM is a morbidity classification published by the United
States for classifying diagnoses and reason for visits in all health
care settings.
and
The ICD-10-PCS is a procedure classification published by the United
States for classifying procedures performed in hospital inpatient
health care settings.
Both Sets
In both the ICD-10-CM and ICD-10-PCS coding systems, you can validate the structure of a code with a regular expression, but validating the content (in terms of which specific combinations of letters and numbers are valid) may be technically possible, but is practically infeasible. A lookup table would be a better bet.
ICD-10-CM
From the Conventions section of the guidelines:
Format and Structure:
The ICD-10-CM Tabular List contains categories, subcategories and
codes. Characters for categories, subcategories and codes may be
either a letter or a number. All categories are 3 characters. A
three-character category that has no further subdivision is equivalent
to a code. Subcategories are either 4 or 5 characters. Codes may be 3,
4, 5, 6 or 7 characters. That is, each level of subdivision after a
category is a subcategory. The final level of subdivision is a code.
Codes that have applicable 7th characters are still referred to as
codes, not subcategories. A code that has an applicable 7th character
is considered invalid without the 7th character.
According to this specification, you'd expect a valid regular expression would look like this:
^\w{3,7}$
However, a review of the actual values shows that, in all cases, the first character is an upper case letter, the second character is a digit, and any alphabetic characters in the remaining available positions are upper case as well. As such, you can use this information to more precisely specify what you're validating:
^[A-Z]\d[A-Z\d]{1,5}$
If you want to allow for a possible period in the fourth position followed by up to four more characters as specified by the OP:
^[A-Z]\d[A-Z\d](\.[A-Z\d]{0,4})?$
ICD-10-PCS
From the Conventions section of the guidelines:
One of 34 possible values can be assigned to each axis of
classification in the seven character code: they are the numbers 0
through 9 and the alphabet (except I and O because they are easily
confused with the numbers 1 and 0). The number of unique values used
in an axis of classification differs as needed...As with words in their
context, the meaning of any single value is a combination of its axis
of classification and any preceding values on which it may be
dependent...Within a PCS table, valid codes include all combinations
of choices in characters 4 through 7 contained in the same row of the
table. [For example], 0JHT3VZ is a valid code, and 0JHW3VZ is
not a valid code.
So to validate the structure of an ICD-10-PCS code:
^[A-HJ-NP-Z\d]{7}$
Use this exp simple :
'^([A-TV-Za-tv-z]{1}[0-9]{1}[A-Za-z0-9]{1}|[A-TV-Za-tv-z]{1}[0-9]{1}[A-Za-z0-9]{1}.[A-Za-z0-9]{1,4})$'
Im looking for a regex that checks for:
a minimum of 2 numbers and
a minimum of 1 capital letter and
a minimum of 4 lowercase letters
And also checks for:
a maximum of 30 characters
I've been trying to make this but all my creations don't work :)
You can leave the maximum out too if you can't do it, I could check it in another way.
I guess the order of those conditions is arbitrary. Therefore there is no need to do it using just 1 regexp. For each condition, you can have 1 regexp and then you can do a logical conjunction in your favorite language and it would be much more readable than one super-cool-ninja-regexp.
a minimum of 2 numbers
".*\d.*\d.*"
a minimum of 1 capital letter and
".*[A-Z].*"
a minimum of 4 lowercase letters
".*[a-z].*[a-z].*[a-z].*[a-z]"
a maximum of 30 characters
".{6,30}"
^(?=.*\d.*\d)(?=.*[A-Z])(?=.*[a-z].*[a-z].*[a-z].*[a-z]).{7,30}$
But if you want only the alphanumerics, then:
^(?=.*\d.*\d)(?=.*[A-Z])(?=.*[a-z].*[a-z].*[a-z].*[a-z])[a-zA-Z0-9]{7,30}$
(?=.*\d.*\d): at least two digits
(?=.*[A-Z]): one caps letter
(?=.*[a-z].*[a-z].*[a-z].*[a-z]): minimum four lowercase
[a-zA-Z0-9]{7,30}: length between 7-30
Well I tried to sum it up in the title.
I need a reg ex to match numbers and commas, but not numbers starting with 0 unless it's 0,number
My users enter hours in a field, so they have to be able to enter 0,3 hours, but they are not allowed to write 002 or 09.
I have this reg ex
^[0-9]*\,?[0-9]+$
How can I extend it to not allow start with 0 unless the 0 is followed by a comma
Another one :)
^(0|[1-9]\d*(|,\d+)|0,\d+)$
This one should suit your needs:
^0,\d*[1-9]|[1-9]\d*$
either 0,\d*[1-9]: a 0, followed by a comma, followed by 0 or more digit, followed by one digit between 1 and 9
or [1-9]\d*: a digit between 1 and 9, followed by zero or more digit
Matches:
0,3
0,03
3
30
Doesn't match:
0
0,0
0,30
03
You don't need to force everything into a single regex to do this.
It will be far clearer if you use multiple regexes, each one making a specific check.
if ( /^[0-9]+,[0-9]+$/ || /^[1-9][0-9]*$/ )
Here we are making two different checks. "Either this one matches, or the other one matches", and then you don't have to jam both conditions into one regex.
Let the expressive form of your host language be used, rather than trying to cram logic into a regex.