Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Tweak for my Perl Regex that screens for digits only

by hackermike (Novice)
on Jan 25, 2006 at 18:36 UTC ( [id://525519]=note: print w/replies, xml ) Need Help??


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

Hi! a nearly as I can tell, this reply does not address the point of the regex intention:
It is * NOT * intended as validation for a phone number. It is intended to dis- allow ANY non digit charcters except spaces parens and hypens which are/can be used in phone # formatting.
When text or letters are input the regex generates an error msg, so it does not appear to "match" input with letters. My question is only, how to allow ONLY certain word characters, specifically, ext. and/or Ext.?
thx
mike
---------------------
I have this regex in my simple perl script handling a html form:
<code> unless ($FORM{'phone'} =~ /\s*\(*\)*\.*\d+\-*\s*/) { <code> ... the point of which is to disallow text in the field whilst allowing for various punctuation styles. Hoping to avoid re-inventing the whole system, I'd like to be able to allow certain text, i.e. ext. or Ext. Any ideas?
thx mike
  • Comment on Re^2: Tweak for my Perl Regex that screens for digits only

Replies are listed 'Best First'.
Re^3: Tweak for my Perl Regex that screens for digits only
by Roy Johnson (Monsignor) on Jan 25, 2006 at 18:48 UTC
    You want to list all the alternatives, separated by vertical bars. For individual character alternatives, you can create a character class (a list or range of characters inside square brackets). For example:
    /^(?:[-()\d\s]|[Ee]xt\.)*$/
    matches a series of (any combination of) only hyphen, left-paren, right-paren, digits, whitespace, or Ext. or ext.

    Updated: added the hyphen. Note that a hyphen, if it appears in a character class, must be the first listed character (so that it doesn't look like part of a character range).


    Caution: Contents may have been coded under pressure.
      THANKS ROY! Apologies for being slow to acknowledge your reply. I goofed on first testing your code but then on inspection cleared that up and find that your regex does exactly as expected and is what I wanted, simply, and effectively,
      Although I would like to get the hyphen included with the permissible characters, and wonder whether it will require the escape backslash? Thank you for your very kind condsideration of this hapless hacker!
      Mike
      I WROTE:
      I have this regex in my simple perl script handling a html form:
      unless ($FORM{'phone'} =~ /\s*\(*\)*\.*\d+\-*\s*/) {
      ...
      the point of which is to disallow text in the field whilst allowing for various punctuation styles. Hoping to avoid re-inventing the whole system, I'd like to be able to allow certain text, i.e. ext. or Ext. Any ideas? thx, mike

      YOU WROTE:
      You want to list all the alternatives, separated by vertical bars. For individual character alternatives, you can create a character class (a list or range of characters inside square brackets).
      For example:
      /^(?:()\d\s|Eext\.)*$/
      matches a series of (any combination of) only left-paren, right-paren, digits, whitespace, or Ext. or ext.
       /^(?:[-()\d\s]|[Ee]xt\.)*$/
      matches a series of (any combination of) only hyphen, left-paren, right-paren, digits, whitespace, or Ext. or ext. Updated: added the hyphen. Note that a hyphen, if it appears in a character class, must be the first listed character (so that it doesn't look like part of a character range).

      THANKS!
      My seeking help with making the ext. permissible actually solved two problems, as ikegami pointed out, my poor effort was not even doing what I thought it would!

      Now if there only some way for perl to limit the number of characters in a text field, (not a text area and not the phone field)so they can't enter whole paragraphs in the title field............
      Mike
        You can truncate a string variable to whatever length you like by using substr.
        substr($text_field, 100) = '' if length($text_field) > 100;
        will truncate $text_field to 100 chars. Or you could have your program just abort if it received an oversized field.

        Caution: Contents may have been coded under pressure.
Re^3: Tweak for my Perl Regex that screens for digits only
by ikegami (Patriarch) on Jan 25, 2006 at 19:35 UTC
    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
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://525519]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-18 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found