Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Regex Dollar Amounts by andreychek
in thread Regex Dollar Amounts by Additude

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2024-04-25 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found