Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Need help with regexes to validate user input

by Anonymous Monk
on Nov 11, 2003 at 04:32 UTC ( [id://306062]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, my name is Audrey and I have been a huge fan of this board during my time in college. But nonetheless, I have stumbled onto a big problem with my PERL (Regex) code. This is what I have so far:
#!/usr/bin/perl use strict; use CGI qw(:standard); #n +otifies that CGI will be implemented. print header, start_html("Form"), #start of HTML. h1("Form"); if (param()) { #variable definitions. my $who = param("firstname"); my $who2 = param("lastname"); my $email = param("email"); my $profession = param("profession"); my $creditcard = param("creditcard"); my $creditcardnumber = param("creditcardnumber"); my $question = param("question"); print p("Thank You. Here is your receipt:"); print p("Name: $who $who2"); print p("E- mail: $email"); print p("Profession: $profession"); print p("Credit Card: $creditcard"); print p("Credit Card Number: $creditcardnumber"); print p("Question: $question"); } else { print hr(), start_form(); print p("What's your first name?", textfield("firstname")); print p("What's your last name?", textfield("lastname")); print p("Enter your e- mail address", textfield("email")); print p("What is your profession?", popup_menu("profession", ['Pim +pin', 'Hustlin n Bustlin', 'Crackalakin', 'Playa', 'Thuggin n Buggin' +])); print p("What type of credit card will you be using?", radio_group +("creditcard", ['Asiv', 'Retsam Card', 'Gotham Express'])); print p("Please enter your credit card number:", textfield("credit +cardnumber")); print p("Choose a question:", checkbox_group("question", ['Do you +understand the words coming out of my mouth?', 'Go ahead, want to mak +e my day, punk?', 'Are you ready to get funkafied?'])); print p(submit("Submit"), reset("Reset")); print end_form(), hr(); } print end_html
Now, I need to know how to implement regular expressions (regex) into making the first and last names containing only alphabetical characters. Secondly, the email address must be valid, in that the input should contain an "@" sign. Thirdly, the credit card number should only contain 16 characters. If the user does not satisfy these requirements, they should be prompted back to a resetted page. I know I asking for a lot, but I really hope you guys can help.

Love always,
Audrey

Edit by tye, title, preserve formatting, etc.

Replies are listed 'Best First'.
Re: Audrey Fong
by ehdonhon (Curate) on Nov 11, 2003 at 04:43 UTC
Re: Audrey Fong
by ysth (Canon) on Nov 11, 2003 at 04:45 UTC
    May I suggest a quick read through perldoc perlrequick? Anyway here are some ideas to get started:
    if ($name =~ /^[a-z]+$/i) { print "only alphas" }
    but you probably want to allow certain other characters such as - (my name has one) or . (e.g. Sammy Davis Jr.).

    Alternatively, instead of matching the whole string as a sequence of alphabetic characters, look for any non-alphabetic characters:

    if ($name !~ /[^a-z]/i) { print "no non-alphas" }
    I would tend to use a regex for the name matching (so that I could specify that the first character be a letter, not a . or - or whatever else I ended up allowing) but for the other two problems, use index and length, respectively. They do have fairly simple regex solutions, though.
Re: Audrey Fong
by Roger (Parson) on Nov 11, 2003 at 04:42 UTC
    1. alphabetical characters check

    if ($who !~ /[A-Za-z]/) ... # thanks to eric256 to point out the prob +lem if ($who2 !~ /[A-Za-z]/) ... # in my regexp. I really meant to use a n +egative # regexp. If any character not alphabetic +al. ^_^
    2. email address validation

    if ($email =~ /@/) ...
    Ok, what I did was perhaps too simple here. You should really use the Mail::Address module to check/obtain the canonical form of the Email address properly.

    3. credit card number should only contain 16 characters

    if ($creditcard =~ /^\d{16}$/) ...
    Assume no space in the credit card number. Otherwise a simple check to strip out the space. Thanks to hardburn to point out the credit card checking modules. I was a bit lazy because Audrey Fong only wanted to check if there were 16 digits.

      3. credit card number should only contain 16 characters

      if ($creditcard =~ /^\d{16}$/) ...

      Assume no space in the credit card number. Otherwise a simple check to strip out the space.

      Actually, for CC validation, use Buisness::CreditCard. A CC num isn't necessiarily 16 digits (it changes between companies), the first few digits identify the company, and the last digit is a checksum. Buisness::CreditCard will look at all these things for you.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

      That name solution just checkes to see if there are letters in it, not that there are only letters in it. One regex to test that would be $who =~ /^([a-zA-Z])$/. That would give you the flexibility to later require it to start or end with certain letters, allow a - in the middle or a .

      Example of how the two regex differ when matching a string.

      use strict; my $test1 = "abcd"; print "Testing: $test1\n"; print "Passed #1\n" if ($test1 =~ /[A-Za-z]/); print "Passed #2\n" if ($test1 =~ /^([A-Za-z])*$/); $test1 = "abcd1234"; print "\nTesting: $test1\n"; print "Passed #1\n" if ($test1 =~ /[A-Za-z]/); print "Passed #2\n" if ($test1 =~ /^([A-Za-z])*$/); # output __DATA__ Testing: abcd Passed #1 Passed #2 Testing: abcd1234 Passed #1

      ___________
      Eric Hodges
Re: Need help with regexes to validate user input
by Anonymous Monk on Nov 11, 2003 at 07:33 UTC
    Oh, I may have misread your comments. But could anyone help me out as to where I need to implement the "If/Print" statements above into my existing code? Thanks. Audrey.
      No, my comment was ambiguous, sorry.

      Anyway, right now your code has three major parts: if there are params, it is processing form input and then displaying a receipt; otherwise it displays the form. The validation goes in the "processing form input" part. If any of your validations fail, you want to print an error message, skip displaying the receipt, and go on to display the form. I hope that helps, or at least gets you on to the next specific question.

Re: Need help with regexes to validate user input
by Anonymous Monk on Nov 11, 2003 at 07:13 UTC
    Hi, it's Audrey. thank you ysth for commenting. Yes, I would appreciate if someone could implement what I need into my existing code. That way, I can test it out on a server and hopefully can figue it out. Thank you so much.
      I'm sorry, I was trying to explain why I thought that would be a bad idea.
Re: Audrey Fong
by Anonymous Monk on Nov 11, 2003 at 04:46 UTC
    Hi, it's Audrey. Thank you guys so much for the fast help. I'm sorry, but I still do not understand your comment. Is there any possible way you can clearify it. Thanks a bunch.
      Can you clarify what you don't understand? If not, perhaps you could read some tutorials and come up with some specific questions? It would be possible for someone else to rewrite your code to do the checks you suggest, but that would leave you stuck with using code you might not understand.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found