# First let's remove all non-digits $phone =~ tr/0-9//dc; # next check the length if (length($phone) != 4 and length($phone) != 10) { # do something with an incorrect phone number } else { # do something with a correct phone number }