Forum Moderators: coopster & phranque

Message Too Old, No Replies

regex numbers and decimals

should be an easy one

         

idiotgirl

1:23 am on Jan 4, 2002 (gmt 0)

10+ Year Member Top Contributors Of The Month



I want to be sure that a particular form field contains only numerals, optionally include ONE decimal point followed by no more than two digits, and NOT allow the dollar $ symbol. No commas or other funny stuff allowed.

I know it's simple (regexes are hard for me), but the Regex book I bought awhile back is literally buried and MIA someplace in my office and it isn't something I can look up at the library just this minute.

Appreciate GREATLY.

Key_Master

2:02 am on Jan 4, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if ($total =~ /^(\d+)\.(\d\d)$/) {
$total = "$1\.$2";
}
elsif ($total =~ /^(\d+)$/) {
$total = "$1\.00";
}
else {
Send visitor back to fill out form again
}

This is untested but I think it will work.

idiotgirl

2:20 am on Jan 4, 2002 (gmt 0)

10+ Year Member Top Contributors Of The Month



Okay, so value $1 is the value of any digits before a decimal, $2 is the value of anything after a decimal - and if $2 has no value we go for the value of $1 (whether empty or not) and $2 is assigned double zeros. Essentially - no input = .00

Groovy. I'm elated. I'll try it. Thanks!