Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

validate a form field with regexp?

by raggmopp (Novice)
on Apr 21, 2012 at 03:37 UTC ( #966298=perlquestion: print w/replies, xml ) Need Help??
raggmopp has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. Working with perl 5.8.8 on RHEL5

I am working on the remaining fine details of a completed script. One detail is to validate user input in a field of a cgi form.

That field is to contain only SR#######. That is, the 1st character is upper case 'S', the 2nd character is upper case 'R', the remaining 7 characters are numbers, 0-9. Length of the field is = 9, not lower not higher.

I can test for ^SR, I can test for length = 9, but how can I test for '^SR followed by 7 number characters'?

Many thanks!

Replies are listed 'Best First'.
Re: validate a form field with regexp?
by davido (Archbishop) on Apr 21, 2012 at 03:42 UTC

    if( $input =~ m/^(SR\d{7})$/ ) { print "User entered $1: OK.\n"; } else { print "Invalid input.\n"; }

    Have a look at perlretut.


Re: validate a form field with regexp?
by Anonymous Monk on Apr 21, 2012 at 07:06 UTC

    See also Params::Validate, FormValidator::Simple, other validators


    use Regexp::English; print Regexp::English ->new ->beginning_of_string ->literal('SR') ->digit ->digit ->digit ->digit ->digit ->digit ->digit ->end_of_string, "\n"; __END__ (?^:\ASR\d\d\d\d\d\d\d\Z)


    use YAPE::Regex::Explain; print YAPE::Regex::Explain->new( qr{(?:\ASR\d\d\d\d\d\d\d\Z)} )->explain; __END__ The regular expression: (?-imsx:(?:\ASR\d\d\d\d\d\d\d\Z)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- \A the beginning of the string ---------------------------------------------------------------------- SR 'SR' ---------------------------------------------------------------------- \d digits (0-9) ---------------------------------------------------------------------- \d digits (0-9) ---------------------------------------------------------------------- \d digits (0-9) ---------------------------------------------------------------------- \d digits (0-9) ---------------------------------------------------------------------- \d digits (0-9) ---------------------------------------------------------------------- \d digits (0-9) ---------------------------------------------------------------------- \d digits (0-9) ---------------------------------------------------------------------- \Z before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

        You're the first person I've seen use Regexp::English!

        I think it a nice crutch for newbies :) something I can copy/paste that newbies can run with

        FWIW, it needs patching, beginning is laborious to write, start man, start :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://966298]
Approved by lidden
[prospect]: Learning Perl There where 17 videos.
[prospect]: And it was great because I'm using the book as well, to get some more examples or extra information
[ambrus]: Ask merlyn directly. Maybe he has copies.
[ambrus]: write him an email or something
[prospect]: ok I will

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2017-09-25 10:30 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (279 votes). Check out past polls.