Gossamer Forum
Home : General : Perl Programming :

number validation

Quote Reply
number validation
let's say i have fields for 9 scores and 9 putts. i need to check each of these to be sure they're numeric or blank. if a field is blank, i want to change it to zero. then i need to compare the putts to the score to be sure the score is always greater than putts. i have tried a zillion things but cannot get it to work! i'm incrementing the scores in $in{"fs$k"} and putts in $in{"fp$k"} where k increments from 1 to 9. here is excerpt of my code:

Code:
my ($k) = 1;
while ($k < 10 ) {
$in{"fs$k"} =~ s/\s+$//g;
if (!$in{"fs$k"}) {$in{"fs$k"} = '0'; }
...

if (($in{"fs$k"} =~ /^[0-9]+$/) || ($in{"bs$k"} =~ /^[0-9]+$/)) { $score = 'Scores (must be numeric)<br>'; }
if (($in{"fp$k"} =~ /^[0-9]+$/) || ($in{"bp$k"} =~ /^[0-9]+$/)) { $putt1 = 'Putts (must be numeric)<br>'; }
if (($in{"fp$k"} > $in{"fs$k"}) || ($in{"bp$k"} > $in{"bs$k"})) { $putt2 = 'Putts (must be less than score)<br>'; }
++$k;
}
i keep getting the message that scores and putts must be numeric, even when i type numbers in every field and my putts are less than scores. please help!
Quote Reply
Re: [delicia] number validation In reply to
mostly working now but not getting error when putts greater than or equal to score. putts should always be less than score. fp and bp are putts; fs and bs are scores.
Code:
my ($k) = 0;
while ($k < 9 ) {
$in{"fs$k"} =~ s/\s+$//g;
if (!$in{"fs$k"}) {$in{"fs$k"} = "0"; }
$in{"fp$k"} =~ s/\s+$//g;
if (!$in{"fp$k"}) {$in{"fp$k"} = "0"; }
$in{"bs$k"} =~ s/\s+$//g;
if (!$in{"bs$k"}) {$in{"bs$k"} = "0"; }
$in{"bp$k"} =~ s/\s+$//g;
if (!$in{"bp$k"}) {$in{"bp$k"} = "0"; }
unless (($in{"fs$k"} =~ /[0-9]+$/) && ($in{"bs$k"} =~ /[0-9]+$/)) { $score = 'Scores (must be numeric)<br>'; }
unless (($in{"fp$k"} =~ /[0-9]+$/) && ($in{"bp$k"} =~ /[0-9]+$/)) { $putt1 = 'Putts (must be numeric)<br>'; }
unless (($in{"fp$k"} < $in{"fs$k"}) && ($in{"bp$k"} < $in{"bs$k"})) { $putt2 = 'Putts (must be less than score)<br>'; }
++$k;
}
Quote Reply
Re: [delicia] number validation In reply to
Yo're best bet would be to add some debugging in, so you can see what it is actually trying to "compare". Its most likely needing a "chomp" or has a leading/trailing space or similar

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] number validation In reply to
everything is working now. i think i had to test to see if there was a value before i did the comparison. don't remember now, i made so many changes!