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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.