Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    regex for price values - this good?

    Hi,

    I am checking that the value entered in a form is decimal like this: -

    1.00 or
    10.00 or
    35.00 or
    134.88 or
    or 35 (cents)

    this regex seems to work with those scenarios but, I wonder if you know (or would suggest), an alternative which allows either a comma or a stop?

    Code:
     unless ( $price =~ /^\d{0,5}\.?\d{1,2}$/ or $price eq '' )
      {
      print "nope. not enough";
      }
    That works as required with a dot.

    this is my attempt at allowing a dot or a comma
    Code:
    $price =~ /^\d{0,5}[.,]?\d{2}$/ or $price eq '' )
    Y'see, I want to add this afterwards:

    Code:
      unless ( $price =~ /[.,]?/)
      {
      $price = (join ( ' ' , ( $price, 'cents')));
      }
    bazz
    Last edited by bazz; 04-21-2009 at 12:23 AM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •