473,700 Members | 2,608 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 23545

"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
4520
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
5426
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
2943
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 processing the results with the VB application that interogates the DB.
4
9754
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
4595
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 exception from Convert.ToDecimal or decimal.Parse is too slow. Does anyone know of any methods...
1
1318
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 user (as an extreme) types in the following, they should diplay as: 1 display as 0.01
2
1520
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" ContentType="text/html"
4
3698
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 is how .NET is going to
3
3437
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
8731
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8649
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8975
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7819
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5904
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4660
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3097
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
2
2395
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.