SageFix. LetsFix!  
Current Location : Home > Web Development > JavaScript > Regex help Share/Save/Bookmark    

Main Menu
SageFix - Let's Fix

Board Categories
    Web Site Designing
- Graphics
    Web Development
- .NET
- Classic ASP
- JavaScript
- PHP
    WebSite Management
- Promotion Techniques
- Search Engine Optimization
    Databases
- MySQL

Tutorials

Database Connection Strings

  Contact Us


 1 - 8 of 8 [Total 1 Pages]  
Regex help      Jul 23, 2002, 11:08 1
  
I'm making a regex to validate money data, and these are the rules I want: (X will represent numbers)

$ must be at front
.XX -OR- .X is optional for end
. at end NOT valid
1-3 digits left of decimal
infite combinations of XXX, where needed

Examples of valid formats
$xx,xxx.xx
$xxx,xxx,xxx,xxx
$x,xxx,xxx.x
$x.xx

Examples of invalid formats:
$xxxxx
xx,xxx.xx
$xx,xxx.
$xxx,xxx.xxx

Here is what I've got so far, but I know it doesn't work...
Code:
moneyReg = "^//$([0-9]{1,3}[,]{1})*([0-9]{1-3})+(//.[0-9]{1,2})?$";
I've had several iterations that have had some success, but not total. This is where I am now, and it breaks pretty much on everything...my brain is too tired to backtrack.

Thanks in advance!

 
      Jul 23, 2002, 11:20 2
  
Sorry folks...just figured it out
Code:
moneyReg = "^//$(?:(?:[0-9]{1,3},)(?:[0-9]{3},)*[0-9]{3}|[0-9]{1,3})(//.[0-9]{1,2})?$";
Meep, that's a BIG one!

 
      Jul 23, 2002, 14:38 3
  

 
      Jul 24, 2002, 01:52 4
  
/d can replace the [0-9] sections... can't it?

Flawless

 
      Jul 24, 2002, 01:59 5
  
Sure can, but since I'm declaring this regex in a string, I like to avoid as many backslashes as possible, as each one has to be escaped by another backslash. Sounds picky, but starts to look confusing...much easier to see the atoms as [0-9] for me.

 
      Jul 24, 2002, 08:17 6
  
Why declare it in a variable?

still two more characters each time - but that's your preference

Flawless

 
      Jul 24, 2002, 08:30 7
  
Well, for what I'm using this for, some of the values in the regex come from varialbes. Easier to concatenate them as a string first, and then create a new regex object from the string. Like input that must be a number, variable in length
Code:
var lBound = 2;var uBound = 6;var numReg ="/^[0-9]{"+lBound+","+uBound+"}$/";var regex = new RegExp(numReg);

 
      Jul 27, 2002, 07:34 8
  
Makes sense now

Flawless

 
 1 - 8 of 8 [Total 1 Pages]  









Subscribe to our mailing list
email


Current Location : Home > Web Development > JavaScript > Regex help Share/Save/Bookmark    

Development problem? SageFix is completely free -- paid for by advertisers and donations. Click here to Contact Us If you have any query. Enjoy!
Request processed in 7.0E-5 seconds Home - Contact Us - Terms Of Service - Privacy Policy - Copyrights - Top
Advertisements do not imply our endorsement of that product or service.
Current server time now is 18-Mar-2010 06:17:50
Copyright © 2009 Sagefix Inc. All rights reserved.
Powered By SageFix