Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

validate number syntax

validate number syntax

validate number syntax

i am trying to validate a price field as any number of numerals (but not required like $.99) followed by a decimal (but no required like $9)followed by 1 or 2 numerals (but not required like $9 or $9.).

i start by removing leading and trailing white space and stripping spaces, commas and $ sign, then check what's left


# should only contain numbers and decimals
$price =~ m/[^0-9\.]/

# should follow the pattern described above.....
$price =~ m/([0-9]+)?(\.{1})?([0-9]{1,2})?/

if the value passes i use sprintf to properly format to 2 decimals.

everything works except when 3 numerals are entered after the decimal it passes.

why doesn't it fail since i include {1,2} in the code?



RE: validate number syntax


Your question has nothing to do with CGI, it is just plain perl.

Regarding your regular expressions :


# should only contain numbers and decimals
# true if $price contains at least one character that is not a digit or a dot
$price =~ m/[^0-9\.]/

# should follow the pattern described above.....
# always true
$price =~ m/([0-9]+)?(\.{1})?([0-9]{1,2})?/
Better take a look at man perlfaq4 | How do I determine whether a scalar is a number/whole/integer/float?.


RE: validate number syntax

As feherke said, this is a pure Perl question.  In the future, you'll probably get better response there.

For now, you should realize that, in your first pattern match, $price =~ m/[^0-9\.]/, when the first character in your character class, those inside the square brackets [], is an up carat, it turns the class into a negative.  So, instead of looking for anythign that is a 0-9 or a decimal, you are looking for anything that is not one of the chars in your char class.  

I have not played with you second pattern match, but, I suspect it will work if you put a $ as the last char in the match.  That pins the match to the end of the string you are matching.  Like
$price =~ m/([0-9]+)?(\.{1})?([0-9]{1,2})?$/

I would also put an up carat at the beginning to pin down that start of your match with the start of the string.  Like,
$price =~ m/^([0-9]+)?(\.{1})?([0-9]{1,2})?$/

Without the $, you are asking if the string has 1 or 2 chars from the character class [0-9].  A string with 3 chars does, in fact 1 and 2 chars from that char class, thus it passes the match expression.

With the $ you are asking if the string has 1 or 2 chars from the character class [0-9] and then ends.  A string with 3 chars after the decimal will fail this match.


'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close