On Mon, Apr 23, 2001 at 11:38:42AM -0400, David Gilden wrote:
> Is there a better way of doing this?

Hmmm.  Where to start?  :-)

> Thanks!
> Dave
> ----
> 
> 
> if ($in{'price'} eq "") {print '<font color="red">&#8226; Missing</font> '; }  
>  elsif (&checkPrice($in{'price'})) {
>   print '<font color="red">&#8226; Wrong Format</font>';
> }
> 
> ....
> 
> 
> sub checkPrice{
> 
> if ($_[0] =~ /\$?(\d*)(\.\d{0,2})?/) {
> return 1; 
>     } else {
> return 0;
>     }
> 
> }
> 

First, a couple of style issues:

 - There is (now) (normally) no need to quote the keys when accessing a hash.

   $in{price} will do fine.

 - Code of the form

   if (x) { return 1 } else { return 0 }    is the same as
   return x ? 1 : 0                         but you normally mean
   return x


Now to the regex.

> Allowable input: 
> $1
> $1.50
> 2
> .50
> $.50
> -----
> Not allowable input: 
> a
> $5a
> $e.40
> ------

Your regex doesn't quite cut it at the moment. Try

  /^\$?(\d+|\d*\.\d\d)$/

I _think_ that's OK.  Think about:

$
a$1


Finally, I think your elsif clause is backwards.  This leaves:

    if (!length $in{price})
    {
        print '<font color="red">&#8226; Missing</font> ';
    }
    elsif ($in{price} !~ /^\$?(\d+|\d*\.\d\d)$/)
    {
        print '<font color="red">&#8226; Wrong Format</font>';
    }


.... possibly.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

Reply via email to