perl regexp BASIC

brok21k
brok21k used Ask the Experts™
on
 Im new with perl. I creating a registration page, my problem lies in validating input data (which i want to acheive through perl)."if($fields[$i] !~ /[aA-zZ0-9]+/)"
I basicly , only want to only allow numeric and char, to be allowed, but it still accepts *-()" ect..

Can any one help.

i.e.
      #Post-code
      if($i == 3)
      {
         
           if($fields[$i] !~ /[aA-zZ0-9]+/)
          {
             
              $errors1[$i] = 6;
           }
           if ($fields[$i] == "")
           {
              $errors1[$i] = 0;
           }
     
view this page for information.
http://johnboshelle.tripod.com/cgi-bin/cwk1.pl
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2014
Top Expert 2015
Commented:
$fields[$i] !~ /^[aA-zZ0-9]+$/)

also, $fields[$i] == "" should be $fields[$i] eq ""

Commented:
brok21k's code will technically match any line that does not contain at least one of the characters in his character class (all strange characters would not work, but if you had one valid character in the field, it would - regardless of what else is in the string).  

ozo's code will match any line that does not start and end w/ one of the characters in his character class (this does not take the newline at the end or spaces into account - one long word would work, but a sentence would not match that).

Try this:
---
unless ($fields[$i] =~ /[\W\S]/) {
---
('[\W\S] == [^ \r\t\n\fa-zA-Z0-9_]')

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial