Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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!

Comment on validate a form field with regexp?
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.


    Dave

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

    Regexp::English

    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)

    YAPE::Regex::Explain

    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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://966298]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-07-30 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (240 votes), past polls