Validate datestring "dd.MM.yyyy" to see if input is numeric - c#

I have a textbox containing a date in the format "dd.MM.yyyy". Now I want to use a RegularExpressionValidator to see if the format matches "digit digit . digit digit . digit digit digit digit" (without spaces)
How does the regular expression look like for this?

Well, the regex "\d{2}\.\d{2}\.\d{4}" should work - but it won't validate that the values are sensible. For example, it will allow "55.66.3000".

Related

asp.net c# Regular Expression Validator for formatted numbers

I have a textbox with a numeric value. For example The number is 23542.56. The number is stored as double in an MySQL Database.
I convert the number to decimal and format it as string while I load the value from the database with thousands separator with ...ToString("N").
TextBox.Text = Convert.ToDecimal(mdr["Value"].ToString()).ToString("N");
My Regular Expression Validator accepts only digits and commas:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox" ValidationExpression="^\d+(\,\d+$)?$" ValidationGroup="NumericValidate">Allowed Chars are: 0-9 und ,</asp:RegularExpressionValidator>
The problem is that the Validator does not accept the formatted number, for exampel 23.542,56. What is a proper way to make him accept only "0-9 and ," but also accept the thousands separator?
Thanks in advance...
Info: To show "28542.56" as "23.542,56" is the common notation in germany, that is the reason why I format the number this way.
You don't need to escape the comma , but you have to escape the dot ., otherwise it will match every character, as in .*, which matches everything.
The [ square brackets ] match any character in the given class, which contains dot ., comma , and digits.
If you want to be more strict and have triplets and such you must do:
thousands v v literal comma
^\d{1,3}(?:\.\d{3})*(?:,\d*)?$
^ ^ non-capturing-groups
This will still match ill-formed numbers with leading zeroes 0 such as:
00
01
003.999,99
There's a very easy way to exclude those, too. I leave it to you as homework :)
Hint: [1-9]
EDIT: accept only 1 1,11 1.111 1.111,11 1111,11
The regex for this should be:
^1+(?:\.111)*(?:,11)?$
It may be a little different for some corner cases, but that's basically it.

before point 3 numbers should accept 0-9 after point decimal 4 digits should accept

I need to validate the text box server side by using regular exp which should accept at most 3 digits [0-9] before the decimal point and 4 digits [0-9] after the decimal point. The part after the decimal point should be restricted to exactly 4 digits (not more or less)
Correct examples:
32.4240
10.0240
100.6400
2.0260
43.0000
The "#" custom format specifier serves as a digit-placeholder symbol. If the value that is being formatted has a digit in the position where the "#" symbol appears in the format string, that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string.
value.ToString("###.####");
If you want to use regexp then try ^\d{1,3}(?:\.\d{1,4})$

Ignore date in a string with numbers using regular expression

I have a little Problem.
i use [0-9\,.]*
to finde a decimal in a string.
And ([^\s]+) to find the text behind the first number.
The string looks normally like this. 1 number a text and than a date:
1.023,45 stück
24.05.10
but sometimes I had just the date and then i become 240510 as decimal.
And sometimes I had just the decimal.
How should I modify the regex to find the date if existing and remove it?
And then look for a decimal an select this if existing.
Thanks in advance.
Divide and conquer
Check for the date first and remove the match from the string
([0-9]{1,2}\.){2}[0-9]{1,2}
Find the number using your original regex
[0-9\,.]*
If you need it find the unit of quantity (assuming that you will only have it as lower case with u Umlaut)
([a-zü]+)
See http://regexe.de/ (German) and http://www.regexr.com/ (English) for some useful information and tools for dealing with regex.
I suggest matching the number in a more restricted way (1-3 digits, then . + 3 digits groups if any, and a decimal separator with digits, optional).
(?s)(?<number>\d{1,3}(?:\.\d{3})*(?:,\d+)?)\s+(.*?)(?:$|\n|(?<date>\d{2}\.?`\d{2}\.?(?:\d{4}|\d{2})))
See demo
The number will be held in ${number}, and the date in ${date}. If the string starts with something very similar to a date (6 or 8 digits with optional periods), it won't be captured. If the date format is known (say, the periods are always present), remove the ?s from \.?s.
(?s) at the beginning will force the period . to match a new line (maybe it is not necessary).

regex for numeric only plus 'h' character only

I want to validate string only if contains numerics and/or 'h' or 'H' character anywhere in the string.
e.g.
123 - valid
123h - valid
1h23 - valid
h234 - valid
123H - valid
asdf - invalid
123d - invalid
I am able to restrict string for numerics only but not with additional requirement of h. how can I do this?
^\d*h?\d*$
Together with the ignore case option.
See it here on Regexr
^ anchor to the start of the string.
\d* match 0 or more digits
h? match 0 or 1 h
\d* match 0 or more digits
$ anchor to the end of the string.
[0-9h]* will match any digits 0-9 or h. To only allow one, "h" you could use [0-9]*h?[0-9]* (I assume you are already doing case-insensitive). You could also limit the number of digits surrounding the h as in [0-9]{1,2}h?[0-9]{1,2}.
Lets get really carried away and assume you want to allow 0-59 to be entered as minutes, or in an hours and minutes format with a separating 'h':
[0-9]{1,2}h[0-5]?[0-9]|[0-5]?[0-9]
This should work for you /[\dh]*/i
Your examples are tested here.
This is the valid regerx:
^\d*h?H?\d*$
Look here : Regex Example
use this:
[^h]*h?[^h]*
this should work.

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