in reply to Re^2: Tweak for my Perl Regex that screens for digits only
in thread Tweak for my Perl Regex that screens for digits only

When text or letters are input the regex generates an error msg,
Disallowing is a form of validation. And your regexp doesn't disallow what you claim it disallows. If there's a digit anywhere in the string, it doesn't generate an error message.
foreach ( '416-967-1111', 'I had 2 glasses of orange juice with my breakfast', 'I had two glasses of orange juice with my breakfast', ) { unless (/\s*\(*\)*\.*\d+\-*\s*/) { print("error message\n"); } else { print("no error message\n"); } }

outputs

no error message no error message error message

rather than the desired

no error message error message error message

Replies are listed 'Best First'.
Re^4: Tweak for my Perl Regex that screens for digits only
by hackermike (Novice) on Jan 25, 2006 at 20:34 UTC
    Thanks for your reply--
    I only meant to say that I'm not trying to validate that the submitted numbers are in fact a phone number.
    You are correct, the "regex" I hacked up does not do what I thought it did. I did not test sufficient possibilites. Thanks for pointing that out.
    Clearly I needed even more assistance than I asked for.
    It's been something of a ,necessary) game deterring the spammers. I don't want to deter the legit posters with too many hurdles, and yet make it too much work for the spammers to be working my forms.
    The testing I could think of indicates Roy's regex will allow whatever numbers are entered but no text other than the ext.
    thx Mike

      Maybe you should consider a statistical approach.

      You could use an absolute tolerance:

      my $good_chars = $FORM{'phone'} =~ tr/-()0-9. //; $good_chars += 3 if $FORM{'phone'} =~ /ext/i; if (length($FORM{'phone'} - $good_chars) > 4) { # Accept <= 4 bad # Bad phone number! ... }

      or a relative tolerance:

      my $good_chars = $FORM{'phone'}) =~ tr/-()0-9. //; $good_chars += 3 if $FORM{'phone'} =~ /ext/i; if ($good_chars / length($FORM{'phone'} < 0.80) { # Accept <= 20% bad # Bad phone number! ... }

      It would probably be more reliable than trying to find out all the valid characters.