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

Hello Monks,

How can I make this validation if statement accept either 11 or 12 digits form the field input?

if ($pw ne ""){ $fieldValid=$pw=~/^\D*\d{4}?\D*\d{4}?\D*\d{4}?\D*$/; if ($fieldValid) { $pw=~s/\D//g } else { $StoresfailedFields="PW";$missingdigitserror = "11 or 12";$info. +="$TheFields $StoresfailedFields $cardfield $requiresfield $missingdi +gitserror $digitsfield<BR>"; $formValid=0 } }

Replies are listed 'Best First'.
Re: validating numerical field
by boo_radley (Parson) on Dec 03, 2002 at 21:51 UTC

    see perlre and the section on standard quantifiers.

    #! perl -l for my $pw (("12345678901","123456789012","1234567890123","whoa")) { print "$pw is ",&validpw ($pw)?undef:"not ", "valid"; } sub validpw { return $_[0]=~/^\d{11,12}$/; }

Re: validating numerical field
by graff (Chancellor) on Dec 05, 2002 at 03:24 UTC
    If you want to accept entries with 11 digits as well as those with 12 digits, and you are expecting some sort of punctuation to be included among the digits, it will be easiest to eliminate the punctuation first, then just check the string length:
    $pw =~ s/\D+//g; $fieldValid = ( length( $pw ) == 11 or length( $pw ) == 12 );