Regular expression with only numbers, dots and minimum value - c#

Im trying to make a regular expression that accept only numbers, dots and that has minimum value and max value.
E.g:
1.000 - valid
100.000 - valid
100.000a -not valid
.10 - not valid
100 - not valid
I have this, which works as i want with numbers and dots, only one thing is missing here, and that is minimum and maximum validation.
#"^([+-]?\d{1,3}(?:,\d{1,3})*(?:\.\d+)*)$"
PS:
Im using data annotations on .net core
Update
I have a javascript that separates users input in input field to thousand format like:
From: 1000000
To: 1.000.000
For better user experience.
But, the problem is the validation with data annotations.
With the RegularExpression i have above is working with the dots, i just need a minimum and maximum value.
I have tried with Range(min, max), but it recognise it as an invalid input because of the dots.
Regards

Given your comment I assume this is what your after:
^[+-]?[1-9]\d{0,2}(?:\.\d{3})+$
It matches a range of 1-3 digits (the first not being a 0), then followed by at least one (no upper limit) sequence of . + three digits.
This will match any number larger than one thousand with . as thousand separators. And since your attempt allowed it, this also allows an optional sign in the beginning.
See it in action here at regexstorm. Note! I can't get $ and multiline to work there so a \s is used instead to illustrate.

Considering that after every number there is space character in the string
This should do the job :
regex = #"[0-9]{1,3}\.[0-9]{1,5}"
Update :
As the obtained text will be numbers , so you could simply validate them for a range check .
For reference please have a look :regex reference
Let me know , if it doesn't works

Related

Insert - after every second Character

so I'm developimg a Game with Unity 3D using C#. As first step the user has to enter his personal Code, which consists of 5 pairs, where each pairs has 2 characters/numbers (Im validating the characters & numbers separately). Now what I'm trying to achieve is that after every second character there should appear a minus, like you have after every 4th number, when you enter your credit-card number.
Example: 27-05-AB-CD-EF
So now I tried to use a Regular Expression and its working for the first two letters, but somehow the Regex does see the minus as a character too, and then it adds a minus infinitely often. I tried different versions, where i thought that i just allow letters and numbers, but somehow that doesn't work.
Regex.Replace(codeText, "([A-Za-z0-9][^-]){2}", "$0-");
Any guess what might be doing wrong?
Try with this expression "([A-Za-z0-9]){2}(?!-)" where (?!-) is a Zero-width negative lookahead assertion which in case you don't know is an expression that is matched but isn't part of the match value. So this expression matches two characters that aren't followed by -.
https://www.regular-expressions.info/lookaround.html read this page for more information

How to give Regular expression for specific decimal values in c#?

I want regular expression validation for the decimal values which are negative and positive values also.
I tried so many examples with simple regular expression also, but it did not work. so I want full code in c#, not just one line regular expression.
my requirement is like : it can allow for ex:
.1
00.1
+.1
+1.0000
-.1
-0.1
-.2311
-23.45
So numbers with + symbol values and - ve symbol values and with out +,- values too.
it should accept starts with . (i.e point starting ex: .01, .345 etc. )
it should accept only 2 digits before the point symbol. (ex: 00.1,+12.100,-12.1 etc)
it should accept any number of digits after decimal ( point symbol) ex: 90.0956546,23.12233451 etc.
Thanks.
I tried above one. Its accepting letters also.
I tried the below one.
#"^[\+-]?[0-9]{0,2}(\.[0-9]{1,9})?$";
This one is satisfying all the conditions.

RegEx to check validate if string has 0 or more numerical values & other items

I'm very much a beginner at Regular Expressions, so I'm stuck on something that should possibly be fairly simple - or maybe it isn't, I just wouldn't know. Also apologies for the fact that I will be putting in step-by-step what my thought process was, not sure if it actually helps.
Anyway I want to validate that a value entered by the user conforms to certain standards that I lay out. These are:
A maximum length of 10
A minimum length of 4
At least 4 letters
A variable minimum of numbers (currently 0)
So theoretically the value of aa1aa2 would pass.
Initially I started with the following, it validated 4 lower case or 4 upper case.
[a-z]{4}|[A-Z]{4}$
But this didn't allow for either/or, which lead me to update it
([a-z]|[A-Z]){4}$
To validate whether it contained the necessary minimum of numbers I changed it to:
(([a-z]|[A-Z]){4})([0-9]{0})$
I could bore people by putting in different attempts that worked / failed under certain scenarios, but basically I started to notice that it only validated instances where there were 4 letters and a number, I eventually (with help from a colleague) came up with this:
^(?!^[0-9]*$)(?!^[a-zA-Z]{4}$)^([a-zA-Z0-9]{4,10}$)
This almost works as I'd want it. It will validate the length of the string, and whether there are upper / lower cases, but will only work if I include a number. Any attempts I've made to strip out the 0-0 in the second half (before {4,10}) and adding {0} have not been successful. Basically without a number it won't work!
How can I modify the final snippet so that I can set a clause where there are potentially 0 numbers?
In C# regex engine (as in PCRE), you can use \p{L} to match any lower/upper case Unicode letter. Restrictions are set with the help of look-aheads. Anchors (^ and $) are used to check whole string/line.
Here is a regex that will check your conditions. Right now, it requires exactly 1 digit to be entered:
^(?=.*\p{L}.*\p{L}.*\p{L}.*\p{L}.*$)(?=.*\d.*$).{4,10}$
Explanation:
^ - String/Line start
(?=.*\p{L}.*\p{L}.*\p{L}.*\p{L}.*$) - Positive look-ahead to require at least 4 Unicode letters that may be non-consecutive.
(?=.*\d.*$) - Positive look-ahead to require at least 1 digit
.{4,10} - Any characters, 4 to 10 occurrences
$ - String/line end
See demo on regexstorm.net.
As Dan suggested, I also very much prefer a more readable approach:
static void Main(string[] args)
{
var input = "testtest";
var numDigits = 2;
bool valid = input.Length > 3
&& input.Length < 11
&& input.Where(char.IsLetter).Count() > 3
&& input.Where(char.IsDigit).Count() > numDigits;
}
Using this approach, you could segment the validation, and provide a more helpful response to the user : input must be at least 4 letters etc

Validating Positive number with comma and period

I need a regular expression validation expression that will
ALLOW
positive number(0-9)
, and .
DISALLOW
letter(a-z)
any other letter or symbol except . and ,
for example, on my asp.net text box, if I type anything#!#--, the regular expression validation will disallow it, if I type 10.000,50 or 10,000.50 it should allowed.
I've been trying to use this regex:
^\d+(\.\d\d)?$
but my textbox also must allow , symbol and I tried using only integer regex validation, it did disallow if I type string, but it also disallow . and , symbol while it should allow number(0-9) and also . and , symbol
Don't Use \d to match [0-9] in .NET
First off, in .NET, \d will match any digits in any script, such as:
654۳۲١८৮੪૯୫୬१७੩௮௫౫೮൬൪๘໒໕២៧៦᠖
So you really want to be using [0-9]
Incomplete Spec
You say you want to only allow "digits, commas and periods", but I don't think that's the whole spec. That would be ^[0-9,.]+$, and that would match
...,,,
See demo.
Tweaking the Spec
It's hard to guess what you really want to allow: would 10,1,1,1 be acceptable?
We could start with something like this, to get some fairly well-formed strings:
^(?:[0-9]+(?:[.,][0-9]+)?|[1-9][0-9]{0,2}(?:(?:\.[0-9]{3})*|(?:,[0-9]{3})*)(?:\.[0-9]+)?)$
Play with the demo, see what should and shouldn't match... When you are sure about the final spec, we can tweak the regex.
Sample Matches:
0
12
12.123
12,12
12,123,123
12,123,123.12456
12.125.457.22
Sample Non-Matches:
12,
123.
1,1,1,1
Your regex would be,
(?:\d|[,\.])+
OR
^(?:\d|[,\.])+$
It matches one or more numbers or , or . one or more times.
DEMO
Maybe you can use this one (starts with digit, ends with digit):
(\d+[\,\.])*\d+
If you need more sophisticated price Regex you should use:
(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)?
Edit: To make it more reliable (and dont get 00.50) you can add starting and ending symbol check:
(^|\s)(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)($|\s)?
I think the best regex for your condition will be :
^[\d]+(?:,\d+)*(?:\.\d+)?$
this will validate whatever you like
and at the same time:
not validate:
numbers ending in ,
numbers ending in .
numbers having . before comma
numbers having more than one decimal points
check out the demo here : http://regex101.com/r/zI0mJ4
Your format is a bit strange as it is not a standard format.
My first thought was to put a float instead of a string and put a Range validation attribute to avoid negative number.
But because of formatting, not sure it would work.
Another way is the regex, of course.
The one you propose means :
"some numbers then possibly a group formed by a dot and two numbers exactly".
This is not what you exepected.
Strictly fitted your example of a number lower than 100,000.99 one regex could be :
^[0-9]{1-2}[\.,][0-9]{3}([\.,][0-9]{1-2})?$
A more global regex, that accept all positive numbers is the one posted by Avinash Raj : (?:\d|[,\.])+

Regular expression for adding special character to phone number

I have added the following regular expression for validating a mobile phone number:
(^07[1,2,3,4,5,7,8,9][0-9]{7,8}$)
I want to allow the user to enter a # character too and I'm not sure where to fit it in. They may need to enter # character after they have dialed a number, or at the beginning of a number to dial a direct number or an extension.
First, your current regex matches 'numbers' of the format 07,12345678 as well. So you need to change [1,2,3,4,5,7,8,9] to [1-9] (when you have a - between two characters in a character class, it usually means that there's a range)
If you want to accept an optional # character, you can use the ? quantifier which means 0 or 1 times.
^#?07[1-9][0-9]{7,8}#?$
regex101 demo
Except that, as you can see in the demo, it will also match numbers with two hashes; one at the front and one at the end. One option to circumvent this is to use some conditionals (which C# can support).
^(#)?07[1-9][0-9]{7,8}(?(1)|#?)$
regex101 demo
(?(1)|#?) basically means that if the first hash was matched, then nothing more should be matched. Otherwise, if no hash was initially matched, then it can match a hash, if there is one at the end of the number.
In C#, it will be a bit like this:
Regex.Match(myString, #"^(#)?07[1-9][0-9]{7,8}(?(1)|#?)$");
Or you could use a negative lookahead to make sure there's never more than one hash in the number:
^(?!.*#.*#.*$)#?07[1-9][0-9]{7,8}#?$

Categories

Resources