How to set a regular expression for amount - c#

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}

Related

Regular expression for specific combination of alphabets and numbers

I am trying to create regular expression for following type of strings:
combination of the prefix (XI/ YV/ XD/ YQ/ XZ), numerical digits only, and either no ‘Z’ or a ‘Z’ suffix.
For example, XD35Z should pass but XD01HW should not pass.
So far I tried following:
#"XD\d+Z?" - XD35Z passes but unfortunately it also works for XD01HW
#"XD\d+$Z" - XD01HW fails which is what I want but XD35Z also fails
I have also tried #"XD\d{1,}Z"? but it did not work
I need a single regex which will give me appropriate results for both types of strings.
Try this regex:
^(XI|YV|XD|YQ|XZ){1}\d+Z{0,1}$
I'm using quantifying braces to explicitly limit the allowed numbers of each character/group. And the ^ and $ anchors make sure that the regex matches only the whole line (string).
Broken into logical pieces this regex checks
^(XI|YV|XD|YQ|XZ){1} Starts with exactly one of the allowed prefixes
\d+ Is follow by one or more digits
Z{0,1}$ Ends with between 0 and 1 Z
You're misusing the $ which represents the end of the string in the Regex
It should be : #"^XD\d+Z?$" (notice that it appears at the end of the Regex, after the Z?)
The regex following the behaviour you want is:
^(XI|YV|XD|YQ|XZ)\d+Z?$
Explanation:
combination of the prefix (XI/ YV/ XD/ YQ/ XZ)
^(XI|YV|XD|YQ|XZ)
numerical digits only
\d+
‘Z’ or a ‘Z’ suffix
Z?$

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

Restrict all the special characters through a regex

There is a long set of characters that are not allowed to validate an input box of winform app.
So i figured that rather than making the long list that are not allowed make the shorter one that are allowed.
The set that is allowed are (a-z,A-Z, 0-9,#,.) .Rest every thing that can be entered are not allowed.
This is the regex that i have made for this.
Regex.IsMatch(textBox1.Text, #"[#\.\w]+$")
It seem to work in some cases but when i enter the data in this format normal character or number special character normal character or number it seems to break few example ee(vv, 55)44,aba&3B.
Not able to figure out whats wrong in this.
Your regex is not valid, because you don't validate all string, but the last part.
You should start it with ^ - beginning of the line symbol.
Regex.IsMatch(textBox1.Text, #"^[\w#.]*$")
\w also means letters in every language, so it will validate the string "абц" too.
So if you need only for english, use
Regex.IsMatch(textBox1.Text, #"^[a-zA-Z0-9#.]*$")
Try this :
Regex.IsMatch(textBox1.Text, #"^[a-zA-Z0-9#.]*$")
Use
^[-a-zA-Z0-9 _ - \. #]*
as the Regex expression text.

match any a-z/A-Z and - character after certain regular expression

i need a certain string to be in this format:
[0000] anyword
so between the [] brackets i need 4 numbers, followed by a whitespace. after that only characters ranging from a to z and - characters are allowed.
so this should be allowed:
[0000] foo-bar
[0000] foo
[0000] foo-bar-foo
etc..
so far i have this:
\[[0-9]{4}\]\s
this matches the [0000] , so it maches the brackets with 4 numbers in it and the whitespace.
i can't seem to find something that allows charachters after that. i've tried putting a single "." at the end of the expression as this should match any character but this doesnt seem to be working.
\[[0-9]{4}\]\s^[A-Z]+[a-zA-Z]*$
the above isn't working either..
i need this expression as a Validationexpression for an asp.net custom validator.
any help will be appreciated
(\[[0-9]{4}\])\s+([A-z\-]+) should hopefully work. It'll capture the numbers and letters into two capture groups as well.
This works for your input: http://regexr.com/?30sb7. Unlike Cornstalk's answer it does not capture anything, and - can indeed be placed later in a range if it's escaped.
Try this one
#"\[[0-9]{4}\] [a-zA-Z]+(-[a-zA-Z]+)*"

C# RegEx for decimal number

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.

Categories

Resources