asp.net c# Regular Expression Validator for formatted numbers - c#

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.

Related

Best way to parse ASCII(?) from a hex string in C#

the string I get in the application includes ASCII(?) characters like !,dp,\b,(,s#.
These are suppose to be equivalent.
value in database-
\x01\x01\x03!\xea\x01\x00\x00dP\x00\x00\x1f\x8b\b\x00\x00\x00\x00\x00\x04\x00\xe3\xe6\x10\x11\x98\xc3(\xc1\xa2\xc0\xa8\xc0\xa0 \x02\xc4\x0c\x1a\x8c\x1a\x0c\x1as#\x04\x18\xf2\b\x1de\xe6\xe6\xe2\xe2b604\x14`\x94\x98\xc3\ba\x9b\"\xb1M\x80\xec\xc9\x10\xb6\x81\x05\x90=\t\xca6Ab[\x02\xd9\x13\xa1\xea\x8d\x80\xec.\xa8\xb8)\x12\xdb\x0c\xc8n\x81\xaa1\x06\xb2\x1b\x19\xb98A\xe2 \xf5\xb5\x10\xa6\x01\x90Y\rf\x1a\x9a#\x98\x16\b&\xc8\x8cJ\x88Z\x90\x11\xa5\x10Q\x90\xb6\x12\x88(H[1\x84\t\xf2O\xb6\xc0&v\tF\x1e\xa1\a\x8c\xc3\xd9\x8f\x8f\x8d%\x18\x01\xa1\x98\x8d\x97\xea\x01\x00\x00
value I get in my app that includes chracters I don't want-
01010321ea010000645000001f8b0800000000000400e3e6101198c328c1a2c0a8c0a02002c40c1a8c1a0c1a73400418f2081d65e6e6e2e26236303414609498c308619b22b14d80ecc910b68105903d09ca3641625b02d913a1ea8d80ec2ea8b82912db0cc86e81aa3106b21b19b93841e220f5b510a60190590d661a9a2398160826c88c4a885a9011a5105190b6128828485b318409f24fb6c0267609461ea1078cc3d98f8f8d251801a1988d97ea0100000a\n\n"3a1ea8d80ec2ea8b82912db0cc86e81aa3106b21b19b93841e220f5b510a60190590d661a9a2398160826c88c4a885a9011a5105190b6128828485b318409f24fb6c0267609461ea1078cc3d98f8f8d251801a1988d97ea0100000a\n\n"3a1ea8d80ec2ea8b82912db0cc86e81aa3106b21b19b93841e220f5b510a60190590d661a9a2398160826c88c4a885a9011a5105190b6128828485b318409f24fb6c0267609461ea1078cc3d98f8f8d251801a1988d97ea0100000a\n\n
you can see that \x01 is 01 then \x03 is 03 then ! is 21. I want to take out all the non hex values in the second string.
What are chracters like ! and dP. Are they ASCII?
I can remove characters like new line like hexString = hexString.Replace("\n", ""); But I'm not sure if that's the best way to do for all.
3.Comparing the two strings, I see that (=28 and s#=7340 . Is there a table for conversion for this?
My guess is given the quotes around the ouput that the database is displaying non-ASCII (Unicode?) characters as hex (e.g. \x03) and that the actual string contains a single character for each hex formatted display, in which case there is no difference to pick out - the character d is also the hex value \x64, it is just the database chooses to output visible characters as their normal letter - same thing with \t which could be output as \x09 but they choose to use (C) standard control character abbreviations.
Found this:
When it is displayed on screen, redis-cli escapes non-printable characters using the \xHH encoding format, where HH is hexadecimal notation.
In other words,
The cli is just using 3 different methods to display the values in the database field:
The character is printable, output the character (e.g. d, P, !, ").
The character is not printable, but has a C language standard escape sequence, output the escape sequence (e.g. \b, \t, \n).
The character is not printable and has no escape sequence, output the hex for the value of the character (e.g. \x03, \x01, \x00).

textbox validation expression property alphanumerical

I'm working with a simple RegularExpressionValidator. The textbox has to be 14 digits long (exactly 14). So, I use ValidationExpression="\d{14}"></asp:RegularExpressionValidator> but that instance just allow numbers, and I need letters also (to be clear, no special characters or dots, semi-colons, only numbers and letters).
What would fit better than "\d{14}" ?
thanks!
\d replaces only digit characters.
Try with \w which replaces letters and also numbers.
So your expression should be:
<asp ValidationExpression="\w{14}"></asp:RegularExpressionValidator>

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.

C# Regular Expression to match pattern 'n/n/n' where n are just numbers

I am looking for the regular expression to see if a string matches the format
[int]/[int]/[int]
e.g.
'98/4/76542' or '98/04/76542'
PS : I do NOT want to check a date is valid but only that the format matches the 'n/n/n' where n is a number.
Edit : Removed the incoorect and misleading 'nn/nn/nnnn', just n\n\n should be sufficient, apologies.
You probably want this:
"^[0-9]+/[0-9]+/[0-9]+$"
Note that the regular expression [0-9] requires that the digit be one of the following characters: 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9. It excludes foreign characters that are regarded as digits, such as the Arabic-Indic numeral ٨. If you want to allow foreign characters that are considered decimal digits, then use \d instead of [0-9]. Here's a demonstration of the difference.
1/22/333 ٨/٢/٠
^[0-9]+/[0-9]+/[0-9]+$ True False
^\d+/\d+/\d+$ True True
Use this regular expression:
^\d+/\d+/\d+$
This pattern will match digits separated by slashes:
^\d+/\d+/\d+$
The + operator makes sure that there is at least one digit in each component, but there is no upper limit, so for exaple 1/2/33333333333333333333333333333333333333333 is a valid string, but the third component can't be parsed into a 32 bit integer.
You can specify the number of digits so that they fall in a reasonable range, for example:
^\d{1,10}/\d{1,10}/\d{1,10}$
The backslash is the escape character in a regular string, so that would be written "^\\d{1,10}/\\d{1,10}/\\d{1,10}$", but you can also use a # delimited string and write it as #"^\d{1,10}/\d{1,10}/\d{1,10}$".
Edit:
For a date you might want to be more specific about the number of digits, for example:
^\d{1,2}/\d{1,2}/\d{4}$
When you want to get the extra "can this be a valid date check", you may use DateTime validInputDate = DateTime.ParseExact(inputString, #"dd\MM\yyyy");. This will throw if the input string is not a valid date matching the format string given.
(Even you stated yuo don't need it, some other searchers may be happy to find this answer here.)

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

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".

Categories

Resources