http://qs1969.pair.com?node_id=187354

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am writing a script to take data from a web page and validate it. Most of the script is done, including the length validation. The only bit i am stuck on as i do not understand perl all that well is the validation of symbols. The field $username must have either characters i.e 'bob' or characters and digits i.e 'bob1' but not just digits i.e '1234565'. It cannot have any symbols either, only _- are to be accepted i.e for usernames 'bob-jones' or 'tom_frank'. The current piece of code i have below does not do exactly what i want it to do, but if somebody could modify it, so that i can plonk it back into my script i would be eternaly gratefull.
if ($display !~ /^\w*$/) { $message = $message.$errmsg ; $found_err = 1 ; }
Thank-you for reading this, and i know for most of you this is simple - but i'm rubbish at this type of thing.

Replies are listed 'Best First'.
Re: Subscription Stuff
by DamnDirtyApe (Curate) on Aug 03, 2002 at 17:44 UTC
    unless ( ( $display =~ /^[\w-]+$/ ) # Can contain chars, digits, _ +and - && ( $display !~ /^\d+$/ ) # But not just digits ) { # String failed to validate. }

    If you're having trouble with the regexps presented, I suggest you give perlre a good long read -- they only get worse from here. :-)


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: Subscription Stuff
by Zaxo (Archbishop) on Aug 03, 2002 at 17:18 UTC

    You want to match at least one, so use '+' instead of '*' as the quantifier. Having passed that, you want to match a non-digit:

    sub is_word_with_nondigit { # $_ = shift; # /^\w+$/ and /\D/; $_[0] =~ /^\w+$/ and $_[0] =~ /\D/; }

    A different quantifier may be used if you want a minimum length, like /^\w{4,}$/

    Update: Modified code to untaint the sub's argument, instead of checking a copy. If you don't like side effects use the commented-out version.

    After Compline,
    Zaxo

Re: Subscription Stuff
by Cine (Friar) on Aug 03, 2002 at 17:06 UTC
    Whats wrong with all the answers you got in 186609??

    T I M T O W T D I
      i am stupid, i couldn't integrate any of those answers into the script i already have. also i need it to validate wether just number have been entered
Re: Subscription Stuff
by BronzeWing (Monk) on Aug 03, 2002 at 21:40 UTC

    I felt an itch to try to do this in one regex. Admittedly it's a longer string than checking with two short ones, but here's a single regex that I believe does the same thing:

    m/^[\w-]*[A-Z\-\_][\w-]*$/i

    Basically, "There must be a single character that is a letter, a dash, or an underscore. It can also have any number of characters before or after it if none of those characters are anything but word characters (letters or underscores) or dashes."

    -BronzeWing