Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Regex Dollar Amounts

by andreychek (Parson)
on Oct 19, 2001 at 07:22 UTC ( #119872=note: print w/ replies, xml ) Need Help??


in reply to Regex Dollar Amounts

You certainly seem to be on the right track. One thing that immediatly sticks out is that you don't appear to be using strict. That might really help with small things as they show up, as would turning on warnings (-w).

Another problem is that you put a question mark immediatly before the . in your regex. By doing that, the decimal point becomes optional, which doesn't appear to be what you want.

Also, I'm not quite sure what "$lengthdecimal" is.. you're using it in your "if" statement, I'm not sure if it's a misspelling, or if it was simply defined earlier in your code.

With these things in mind, I might try something like this:

# Test to see if we got the expected input if($amount =~ m/^-?(\d+)\.(\d\d)$/) { my $integerpart = $1; my $decimalpart = $2; # Display template for correct input here } else { # Display template for error here }
And just for fun -- another option you have is to do this test with JavaScript. More recent JavaScript versions support regexes. If you want to support older browsers, there are other ways to go about testing input. If you catch the error before if hits your server, it saves a tremendous amount of time, which makes your application faster, which makes your users happier :-)

Good luck!
-Eric


Comment on Re: Regex Dollar Amounts
Download Code
Re: Re: Regex Dollar Amounts
by Additude (Initiate) on Oct 19, 2001 at 15:13 UTC
    Hi
    Thanks for the help!
    I messed up copying my code. Darn.
    Here it is again, done correctly.

    $Amount =~ /-?(\d+)\.?(\d+)/; $intergerpart = $1; $decimalpart = $2; $lengthdecimal = length($decimalpart); if ($lengthdecimal gt 2 || $Amount =~ /[^0-9]\.?[^0-9]/) { display_output($templatefile,); print <<EOF;

    Thanks for the help. I'll give your idea a try.
      Hi Again Eric
      I tried the line
      if($amount =~ m/^-?(\d+)\.(\d\d)$/)
      But this regex is forcing me to put a non digit character in place for it to be true
      If I use $50.00, c50.00, abcd50.00 and so on it will produce a true result and allow the user to pass.
      So here is what I did and it seems to be working
      if ($Amount !~ m/^-?(\d+)\.(\d\d)$/) { display_output($templatefile,);#error page print <<EOF;#good result, move on

      I had to change the =~ to !~ because it didn't like having
       print <<EOF; before the display_output($templatefile,);
      for some reason.
      But it looks like all is well in Dodge City now. Thanks!
      Wes

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://119872]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-12-29 07:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (185 votes), past polls