Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 26th, 2003, 02:43 PM
simonmaduro's Avatar
simonmaduro simonmaduro is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: USA
Posts: 140 simonmaduro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 5 m 4 sec
Reputation Power: 6
Send a message via Yahoo to simonmaduro
Simple Regular Expression matching - Currency

Just need a regular expression to match a currency entered by a user.

The acceptable formats for $100.00 would be:

100.00
100.0
100.
100

I am using this regular expression:

/\d+(\.\d{0,2})?/

But it's letting by things like:

100....
100....00

Being a novice in expression matching, I'm not sure where I'm going wrong.

I thought putting \. would ask for exactly one '.'

Reply With Quote
  #2  
Old September 26th, 2003, 03:03 PM
Mirax's Avatar
Mirax Mirax is offline
Senior Member
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2000
Location: Enschede, The Netherlands
Posts: 1,527 Mirax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 49 m 2 sec
Reputation Power: 10
This may be of help...
__________________
There are 10 types of people in this world - those who understand binary and those who don't...

PHP | MySQL | DevShed Forum Search | Google Search

Reply With Quote
  #3  
Old October 1st, 2003, 09:52 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,696 ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 6 Days 20 h 15 m 13 sec
Reputation Power: 1507
100....
100....00

For both of these, it matches the \d+ part (with 100). After that it looks for \.\d{0,2} but fails. However it sees the ?, realises that the last bit is optional and returns true.

Make use of the 'start' and 'end' characters (^ and $ respectively). Also, use a non-capturing group (?:blah) rather than (blah) - it's more efficient if you don't want to refer to them later, which it seems you don't in this case. Rewritten:
Code:
/^\d+(?:\.\d{0,2})?$/ 

Reply With Quote
  #4  
Old May 23rd, 2008, 05:39 PM
stevej6x7 stevej6x7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Location: Colorado, US
Posts: 1 stevej6x7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 53 sec
Reputation Power: 0
This matches US currency

// max character length $99,888,777,666,555.4444
// same as 99888777666555.4444

^[$]?([0-9][0-9]?([,][0-9]{3}){0,4}([.][0-9]{0,4})?)$|^[$]?([0-9]{1,14})?([.][0-9]{1,4})$|^[$]?[0-9]{1,14}$

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Simple Regular Expression matching - Currency


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT