How can one efficiently validate input fields for specific criteria, such as allowing only letters and spaces but not numbers or special characters?

To efficiently validate input fields for specific criteria, such as allowing only letters and spaces but not numbers or special characters, you can use regular expressions in PHP. Regular expressions provide a powerful way to match patterns in strings. By defining a regular expression pattern that matches only letters and spaces, you can easily validate input fields against this criteria.

$input = "John Doe";

if (preg_match('/^[a-zA-Z\s]+$/', $input)) {
    echo "Input is valid - contains only letters and spaces.";
} else {
    echo "Input is invalid - contains numbers or special characters.";
}