$firstname =~ /^([\w ]+)$/; $firstname = $1; if( length($firstname) < 1){ $firstname = "no valid name provided"; } # now $firstname contains only # numbers, letters, spaces and underscores.