I am working Web API where I need to validate data so I am using regex for that.
In web I have a field where It masking is there for that field that only negative numbers upto 4 digits.
So for same I have put regex in API as below :
[RegularExpression(#"^-\d{0,4}$", ErrorMessage = "The cond_ab field must be negative number and maximum length is 4")]
But its failing for case when I pass 0, then its giving validation message. So i need regex to validate number that it must be negative and upto to maximum 4 digits but don't it should pass if number is 0
Use this expression to accept zero (0) or any negative number up to 4 digits:
#"^(0|-[1-9]\d{0,3})$"
If you also want to accept -0, -0123 and the likes, you can use:
#"^(0|-\d{1,4})$"
Use this expression .
If you pass -12 then it will work if you pass -012 then give error .
^-[1-9]\d{0,4}(.\d*)?$
Related
I need to create a pattern for a "text" type input to only allow a number from 0 to a specific max value and at the same time and validate to a specific number of decimal places.
Quick example:
Max Value = 300.86
Max Decimal Places = 3
Valid inputs:
0
1
300
300.86
300.85
300.850
300.851
.2
0.3333
Invalid inputs:
-1
301
300.87
300.861
1,30.2
1,.0
,.1
Currently I only know how to validate number of decimal places using this pattern:
^[,0-9]*(.\d{1,{0}})?$
Note:
I can't use type=number because I can't use any pattern with that :(
please help
I think something like this, is what you're after:
^(300(?:\.(?:[0-7]\d{0,2}|8(?:[0-5]\d?|60?)?))?|[0-2]?\d{0,2}(?:\.\d{0,3})?)$
See it here at REGEX STORM.
(Had to tweak it there, to end in \r, because REGEX STORM wouldn't match $ with end of line even though multi-line was selected???)
Explanation
It has two parts. The latter [0-2]?\d{0,2}(?:\.\d{0,3})? test for numbers below 300. It optionally starts with 0, 1 or 2 ([0-2]?). Then any two digits can follow (\d{0,2}). Then, optionally, it's followed by a . which, if present, can be followed by up to three digits (decimals) ((?:\.\d{0,3})?).
The first part - 300(?:\.(?:[0-7]\d{0,2}|8(?:[0-5]\d?|60?)?))? - test for when the integer part is exactly 300. It may then optionally be followed by a decimal point, and one out of three cases:
[0-7]\d{0,2} a digit in the range 0 to 7, followed by up to two digits
8(?:[0-5]\d*|60*)? an 8 followed by a digit in the range 0 to 5, optionally followed by a digit (The [0-5]\d? alternation).
or the number 86 and an optionall 0 (the 60? alternation)
These parts are in a capturing group separated by an alternation - |. The whole expression must be at the start of the string (^) (or line with multi-line flag) and at the end of the same ($).
Edit
Done some tweaking 'cause some numbers wrongly failed.
Edit 2
Completely missed "the maximum number of decimals" part. Fixed. (Fooled by the "valid" example 0.3333)
I don't know anything about C# so I'll just have to assume that what I can do in Python, you can find some way to do in C#. I'll give you my suggestion in pseudocode (this is not meant to mirror Python).
maxValue = some number
nDecimals = some number
givenValue = text with some number to be tested
#split number on decimal; remove non-digit characters from each side
leftSideOfDecimal = Replace(pattern = '^(\d{1,3}(,\d{3})*)(\.\d*)$', replacement = '\1', givenValue)
leftSideOfDecimal = Replace(',', '', leftSideOfDecimal)
rightSideOfDecimal = Replace('^(\d{1,3}(,\d{3})*)(\.\d*)$', '\3', givenValue)
rightSideOfDecimal = Replace('\.','',rightSideOfDecimal)
leftSideOfDecimal = pass leftSideOfDecimal to function to change type from text to integer
if leftSideOfDecimal <= maxValue and numberOfCharacters(rightSideOfDecimal) == nDecimals:
do whatever you want
Alternatively, if you have a function by which you can split strings on a given character (like a decimal or comma), then you just split the input on the decimal, clean up the first side as in the code, and then proceed with the last three lines of the code above. That way would save you from having to use a mildly complicated regex.
I'm trying to implement a feature which takes a sequence of digits (U.S. Social Security Numbers) as an argument and returns a collection of SSNs which match the input except for exactly one deviation.
So, the input 123456789 would return:
123356789
193456789
123450789
But would not return 123546789, etc.
I have a system in ASP.NET which does pattern matches on inputs with wildcards, like 123**6789. So I could adapt that, using a loop, to this. But if there was a single regex for this, I would just implement it in SQL and be done with it.
So, is there a regex that will do this without having to call it in a for loop?
Unfortunately I'm not specialist in regular expressions but I think that you can use simple regular expression to check absolute value of subtraction between testing value and your input value. Acceptable result should be exactly 1 digit and 0 or more trailing zeroes. For your example values:
Testing value Input value ABS(Subtraction)
--------------+--------------+------------------
123356789 123456789 100000
193456789 123456789 70000000
123450789 123456789 6000
123456786 123456789 3
I have regular expression validator with expression like:
^\d{1,4}(\,\d{1,3})?$
I wan`t to validate next formats:
Decimal numbers with max 3 decimal places like 0,125 1,15, 0,5 but not(1,1234) that is ok but, user can write 5 digits like (12345, 54321 ... )
Exclude 0, so if user just write 0, there will be an error but user can write 0,5 or 0,125... I don`t know how to manage that
So everything is working fine except scenario 2. I don`t know how to exclude only 0... can I do that with regular expression validator, or should I try with custom validator in function(check if value is 0)
You could prohibit the number from containing all zeros with a negative lookbehind. That would also invalidate all entries that consist of zeros and dots, but that's probably a good thing: it is very likely that since you do not want a standalone zero, you do not want 00, 000, or 0.000 as well:
^(\d{1,4}(\.\d{1,3})?)(?<!^[0\.]+)$
// ^^^^^^^^^^^^
// |||
// The lookbehind part
The part that I added to your expression checks that the string does not consist entirely of zeros and dots. Here is a demo on ideone.
I was wondering if somebody could point me to a regex code that validates for this: ####/##/##
Example: 1990/05/25
The second number 0 can only be 0 or 1 and the number 2 and only be 0, 1, 2, or 3. Other than that all other numbers in this set are allowed (0-9).
The code should validate that there is only 9 or 10 characters in total including the slashes.
If you only want to validate this format, you can use a regex like...
^\d{1,4}\/[01]?\d\/[0-3]\d$
I tested it a bit on some dates here.
This will match:
1990/01/01
2012/13/34
2013/1/39
9999/0/00
But reject:
23121/32/44
12/05/013
013/000/00
If you want to reject invalid dates as well such as 2013/02/29, you can check out this thread.
Try this (edit following Jerry)
[0-2][0-9]{3,3}/[0|1][0-9]/[0-3][0-9]
Mess about with the {a,b} notation to change the length of the general digits, it means between a and b of the preceding expression inclusive. It's unclear in your question where you want the digit flexibility to be.
E.g. to emit 2013/5/29, use
[0-2][0-9]{3,3}/[0|1]{0,1}[0-9]/[0-3][0-9]
For all things regex I have found this website to be an invaluable resource. http://www.regular-expressions.info/reference.html
Specifically this page should get you what you need and contains a full explanation of how to go about validating date input format (not value) via Regular Expressions.
http://www.regular-expressions.info/dates.html
^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$
would match
yyyy-mm-dd
I am trying to compose a regular expression to match a numeric value expressed as a decimal multiple of .25 (ex. 1.25, 14.75).
// Must Match
1.0
1.25
1.250000
1.5
1.500
1.75
1.7500
// Must Not Match
1.2
1.46
1.501
1.99
So far I have the following expression: \d+(\.((0+)|(250*)|(50*)|(750*))). It works when I use online tooling like gskinner.com/regexr. When I use the expression in a validation attribute to seed my EntityFramework db, it produces validation errors:
[RegularExpression(#"^\d+(\.((0+)|(250*)|(50*)|(750*)))$", ErrorMessage = "Hours must be 15 minute increments expressed as decimals (ex. .0, .25, .5, .75)")]
public double Hours { get; set; }
Similar question (I am looking for a way to round the decimal portion of numbers up or down to the nearest .25, .5, .75, or whole number) but I need to use a regular expression to use the above data annotation.
Question:
Anyone see what's wrong with my expression?
Bonus points if you can extend it to support whole numbers (ex. 4 or 4.25 but not 4. or 4.62)
To match such number use regex pattern
(?!0\d)\d+(?:[.](?:25|5|75|0)0*)?(?!\d)
To validate input to be such number use regex pattern
^(?!0\d)\d+(?:[.](?:25|5|75|0)0*)?$
In both cases, the very first part (?!0\d) is optional to disallow match/validate numbers with invalid leading zeros, such as 000003.250, when match would trim them and take just 3.250; validation would fail if this optional part is present in the regex.
This matches whole numbers too:
^\d+(\.(25|5|75|0)0*)?$
I tested it with RegexHero. It has a .NET Regex engine in the backstage. If you're using all test cases together, make sure that you make Multiline option selected, so that ^ and $ symbols match each line individually, not the whole text.