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
You can do that also by simply removing space before you do ctype_alpha.
$fname=str_replace(" ", "", $fname);
if(!ctype_alpha($fname)){
echo "Your name may only contain alphabetical letters";
}
this is what I would do
if (!ctype_alpha(str_replace(' ', '', $fname)))
this allows for spaces only, but if you want to allow more than just spaces, like punctuation or what not, read up on str_replace, it allows for arrays
str_replace(array(' ', "'", '-'), '', $fname)
I'm suggesting this because First Name may have apostrophe and last name may also have dashes
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);
}