Regular expressions for numbers - c#

How can I build a regular expression that checks that it must be a number between 0 - 999999999.
Opted for the range validator.

The simplest thing would be [0-9]{1,9} That will accept all integers between 0 - 999999999.
If you want decimals or scientific notation, that can be done too though it's trickier and you might also want to look into some other tool to validate the number value.

Assuming you would want to match numbers that don't start with zero:
Then [1-9]\d{0,8} should do it.

If you mean control the number which range, can use rangevalidator
MaximumValue Specifies the maximum value of the input control
MinimumValue Specifies the minimum value of the input control
<asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="1"
MaximumValue="999999999"
Type="Number"
EnableClientScript="false"
runat="server" />

You could do this:
\d{1,9}
\d - digit
{1,9} - 1 to 9 repetitions

If you are trying to validate this inside a <asp:RegularExpressionValidator> then you need to do the same as has been hinted at so far in this thread but you also need a ^ and $ on each end so that it doesnt allow other characters to be added in around it.
^\d{1,9}$

Try this:
[0-9]*
It works for me.

Related

Make input field validate a number range and a single character using regular expressions

This is a commonly asked question. But I am not getting the answer that I need. I had a look at MVC Validation make RegularExpression numeric only on string field (among many) for reference but I am not quite there yet.
I have an html5 input field bound to an MVC property. I want to make use of the [RegularExpression()] attribute but I am not getting the output that I need. I need my input to only take the following:
A single number between 0 and 7, or the % character
I tried the following:
[RegularExpression("^[0-7][%]")].
Where am I going wrong?
If you want to use range from 0 to 7. (Only single digit at a time) then use [0-7] and for % use [%]. To concatenate both the requirements(For or condition) use | in between two square brackets.
Your regular expression would be look like this
[RegularExpression("^[0-7]|[%]")]
^ at the starting of regular expressions, suggests that your string should starts with the number.
$ mentioned by #Stephen in comments states that your % must occurs at the end of string or expression.
Now its up to you, if you want string should be in D% format, where D is for digit and %, then use ^ in the beginning and $ at the end of regex. e.g. 5%
if this is not the case, then you can remove ^ and $ from the regex e.g %

Regular expression with only numbers, dots and minimum value

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

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}

Categories

Resources