C# RegEx for decimal number - c#

I'm trying to make a Regular expression for a textbox which should only allow either only digits or decimals. For example, I should be able to enter both 10 and 10,350.
The decimal seperator I'll use is "," and the decimal length don't need a limit.
Anyone knows how I can make such a RegEx?

I'd go for decimal.TryParse() too, but if you really need a RegEx then something like this should work: (\d+(,\d*)?)

Just use decimal.TryParse or double.TryParse.

if you will use it in many place in your project you can make new TextBoxcontrol it accept just number and make ',' when even the '.' clicked, and you can make test on it when it lostfocus()

Here's your regex:
^[\d]+,?[\d]*$
check if it matches what's in your textbox.

Related

I need a regex expression for validating sri lankan vehicle number

My vehicle number is something like this.
1-0001,
10-0010,
111-000
A-3049,
KJ-6825,
AAC-3422
The following expression is the one I found.
^([a-zA-Z]{1,3}|([0-9]{1,3}))-[0-9]{4}
But I want that first three characters should not be all zeros or last four characters not be all zeros how do I make a valid expression ?
^([a-zA-Z]{1,3}|((?!0*-)[0-9]{1,3}))-[0-9]{4}(?<!0{4})
Try this.See demo.
https://regex101.com/r/mS3tQ7/12
The lookahead and lookbehind will make sure that there are no all 0\s at start or all 0's at end.
Also use
^(?>[a-zA-Z]{1,3}|(?!0*-)[0-9]{1,3})-[0-9]{4}(?<!0{4})
To make sure you dont make partial matches.
https://regex101.com/r/mS3tQ7/13
How about this:
^((?!0000)([a-zA-Z]{1,3}|([0-9]{1,3}))-[0-9])|(([a-zA-Z]{1,3}|([0-9]{1,3}))-[0-9])(?!000)
Debuggex Demo
And what about this:
^([0-9]{1,3}|[A-Z]{1,3})-[0-9]{1,4}

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|[,\.])+

Should not allow single zero or 0.##

How do I restrict single zero in a numeric (Decimal may alow) textbox? Textbox can accept any number but it should not accept only zero or "0.##" as value.
For example: "900.55", "200.00" is valid but "0" and "0.105"is invalid.
I tried ^[1-9]\d\.?\d[0-9]* but it accepting the "0" and "0.##"
You're almost there.
^[1-9][0-9]*(\.[0-9]+)?$
The input must
start with a number 1-9
be followed by any sequence of 0-9
and optional:
a dot followed by one or more 0-9.
Notes:
This also disallows 3.. If you don't want that, replace the last + with a *.
You can use \d instead of [0-9]. I've used the latter to stay consistent with [1-9] and keep things simple.
I think you're close, but it may be easier to include the decimal and following digits in a group and make that entire group only allowable once like this one:
^[1-9][0-9]*(\.\d*)?
Also, here's a useful site for testing regular expressions.
I think it would be simpler to do this:
float result;
return float.TryParse(textBox1.Text, out result) && result < 1;
If the textbox only accepts valid numbers, and all you want to assert is that it is > 0. All you really need then is
^[1-9]
or if trailing prefix zero's are allowed
^0*[1-9]
You could alternatively write it using a negative lookahead:
^(?!0\b)\d+(\.\d*)?$
This has the added bonus of accepting numbers with a preceding 0 like 022.
I faced the same situation and solve this.
try out it.
#"[^0]*[0-9].\d{2}$

How to set a regular expression for amount

I have a textbox and in it a value like $8.00 I want to validate this textbox to always check for amount values and not accept letters or anything other than a value in the format of 0.00. How can I achieve this in a RegularExpressionValidator?
Thank you for the help.
The RegEx you are looking for is #"^\d+\.\d\d"
It matches strings with 1+ digits before point and exactly two digits after
If you want it to allow start a string from $, then use #"^\$\d+\.\d\d" or #"^\$?\d+\.\d\d" for optional $.
If you want $ to be separated from digits with spaces then use #"^\$?\s*\d+\.\d\d"
The following regular expressiong will allow numbers in the following format (12345.67, 0, 0.1)
^\d{1,5}(.\d{1,2})?$
I used one of the following before i hope it helps try it.
\d{1,3}(.)\d{1,2}
or
\d{1,3}.\d{2}

Regex after character

I am trying to use a regex to get the value after a character. In this case, the string is m325 and I need to get whatever is after the m.
Can anybody tell me what is wrong with my code?
Regex rgMeter = new Regex("m(.+$");
intMeterID = Convert.ToInt32(rgMeter.Match(strID));
Update:
Thanks for all your answers...for some reason the regex "m(.+)$" is returning the m as well as the string I require. I have tried the Groups example and it returns the data that I want. Why do I need to use Groups to do this?
Apart from the missing ), you oversimplified it a bit. You need to do
Regex rgMeter = new Regex("m(.+)$");
intMeterID = Convert.ToInt32(rgMeter.Match(strID).Groups[1].Value);
(Possibly, you might want to add a check if the Match() matched or not.)
You are missing a closing ). Plus, if you are extracting a number you should limit yourself to digits only, Will avoid trying to parse a faulty string into integer in the next statement.
m(\d*)
"m(.+)$" - there wasn't closed (
Also, you can test it on: http://gskinner.com/RegExr/
What values can appear behind the "m" character?
If it's only an integer, the I should use the solution Shekhar_Pro provided..
If any character, go with the rest :)
The regex you require is /^m(.*)$/
Actually you should use \d or [0-9] if you want match digits.
/^m([0-9]*)$/
and
/^m([0-9]{3})$/
if there are always 3 digits
Looks like you missed the closing )
A simple "m\d*" should do this.. please show us whole string to see the case.

Categories

Resources