473,574 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex for decimal and money

I can validate for simple numbers and decimals with the pattern [^\\d.], but
how to prevent the user from entering something silly like 1.5.6?
Jan 1 '07 #1
3 23529

"Earl" <br******@newsg roups.nospamwro te in message
news:Ow******** ******@TK2MSFTN GP04.phx.gbl...
>I can validate for simple numbers and decimals with the pattern [^\\d.],
but how to prevent the user from entering something silly like 1.5.6?
Try a pattern that will allow only one "." in the number...But I would not
use a regexp to parse a number. You should try the TryParse of the numeric
types to make sure you got a valid number.

Also I would not consider "1.234" a valid Decimal since I am German, and you
would have to use 1,234 here ;) And TryParse will acknolage the local
culture

Jan 1 '07 #2
rdrunner napisał(a):
>
"Earl" <br******@newsg roups.nospamwro te in message
news:Ow******** ******@TK2MSFTN GP04.phx.gbl...
>I can validate for simple numbers and decimals with the pattern
[^\\d.], but how to prevent the user from entering something silly
like 1.5.6?

Try a pattern that will allow only one "." in the number...But I would
not use a regexp to parse a number. You should try the TryParse of the
numeric types to make sure you got a valid number.

Also I would not consider "1.234" a valid Decimal since I am German, and
you would have to use 1,234 here ;) And TryParse will acknolage the
local culture

here is a regex:

"(\+|-)?[0-9]+(\.[0-9]*)?"

if you need to check what is the valid decimal separator use

string s =
@"\+|-)?[0-9][0-9]*(\" +
System.Globaliz ation.CultureIn fo.CurrentCultu re.NumberFormat .CurrencyDecima lSeparator
+ @"[0-9]*)?"

best,
SÅ‚awomir

Jan 3 '07 #3
Thanks to both of you. I'll consider both ideas.

"Slawek" <sp************ ****@o2.plwrote in message
news:en******** **@node4.news.a tman.pl...
rdrunner napisal(a):
>>
"Earl" <br******@newsg roups.nospamwro te in message
news:Ow******* *******@TK2MSFT NGP04.phx.gbl.. .
>>I can validate for simple numbers and decimals with the pattern [^\\d.],
but how to prevent the user from entering something silly like 1.5.6?

Try a pattern that will allow only one "." in the number...But I would
not use a regexp to parse a number. You should try the TryParse of the
numeric types to make sure you got a valid number.

Also I would not consider "1.234" a valid Decimal since I am German, and
you would have to use 1,234 here ;) And TryParse will acknolage the local
culture


here is a regex:

"(\+|-)?[0-9]+(\.[0-9]*)?"

if you need to check what is the valid decimal separator use

string s =
@"\+|-)?[0-9][0-9]*(\" +
System.Globaliz ation.CultureIn fo.CurrentCultu re.NumberFormat .CurrencyDecima lSeparator
+ @"[0-9]*)?"

best,
Slawomir

Jan 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4511
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
4
5418
by: Mark | last post by:
Hi - can anyone give me a sample of how I would ensure a value in a text box was a decimal (it is used for entering money - so has to check for no more than 2 decimal places, but still allow should no decimals be input) - eg. 23 - ok 23.01 - ok 23.1 - ok 23.111 - not valid
11
2923
by: ZRexRider | last post by:
This may be an easy question but I've been reading for about a half hour and experimenting without results. I simply want the results of my query to display a specific field that is typed "money" using + and - The program that consumes the data expects + on positive numbers and - on negative. I was hoping to do it in the view instead of...
4
9725
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a numeric value according to an arbitrary regular expression.
7
4586
by: Paul K | last post by:
I'm writing a small component that needs to be as fast as possible. The component needs to convert a string to decimal during the course of it's processing. However, I need to test the string first to make sure it is numeric. Using the is keyword doesn't work (strings cannot be cast as decimal so false is always returned) and catching an...
1
1314
by: Top Gun | last post by:
I have a textbox that displays money amounts as 999999999.99. The user would like the keyer to either type in a decimal point or, if one is NOT supplied, imply one. Furthermore, when the textbox is populated from the database using a money datatype, it is four decimals to the right, which must be formatted to two. So, as far as keying, if a...
2
1516
by: tshad | last post by:
I am trying to get a regular expression to work and I keep getting the following error: Compiler Error Message: BC30469: Reference to a non-shared member requires an object reference. The page is: ********************************************************************************** <%@ Page Language="VB" trace="true" debug="true"...
4
3694
by: Ronald S. Cook | last post by:
We're designing the data model for a project. The app will be in .NET, the database will be in SQL Server 2005. I'm a little confused on type conversion between the two and which I should choose. I found this good comparison chart: http://msdn2.microsoft.com/en-us/library/ms131092.aspx But for monetary amounts, it looks like decimal...
3
3429
by: Stephan Diehl | last post by:
Hi lazyweb, I'm wondering, if there is a usable money data type for python available. A quick search in pypi and google didn't convey anything, even though the decimal data type seemed to be planned as a money data type originaly. Thanks for any pointers Stephan
0
7814
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8067
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7826
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5307
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3748
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3756
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2252
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.