ctype_alpha but allow spaces(php)

前端 未结 3 635
南方客
南方客 2021-02-09 04:58

I was wondering if one can allow spaces in a textfield when checking it with ctype_alpha. Since ctype_alpha only allows alphabetical letters, I don\'t know how to let the user e

3条回答
  •  暖寄归人
    2021-02-09 05:15

    Removing the spaces is the way to go, but remember ctype_alpha results in a false on an empty string these days! Below the method I use...

    function validateAlpha($valueToValidate, $spaceAllowed = false) {
        if ($spaceAllowed) {
            $valueToValidate = str_replace(' ', '', $valueToValidate);
        }
        if (strlen($valueToValidate) == 0) {
            return true;
        }
        return ctype_alpha($valueToValidate);
    }
    

提交回复
热议问题