View Full Version : Using ctype functions

11-12-2012, 02:51 AM

I am starting to validate my form post data and wondered if there is anyway I can get around using the ctype_alpha() function in allowing a space to be entered in that given string?

I need to be able to let the user enter a space in the 'name' field in-case they try entering their surname.

Do I need to manually search, do something like:

$characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";

//search each position in the input string, if at any time it doesn't contain
//something in the above string, do error. if it does always contain
//a value from the above string, we know its ok?

Thank you for your thoughts.

Kind regards,


11-12-2012, 10:54 AM
I think using a regular expression would be what you want. Perhaps preg_match() (http://php.net/manual/en/function.preg-match.php) is the right function, although I'm not certain of the exact syntax. Maybe it would be like this:

if(preg_match("^([a-z ])*$", $input ) { /* $input contains only letters and spaces. */ }

You can read more here (http://www.php.net/manual/en/book.pcre.php). It would also be good for someone else to check that syntax I used, I'm not too confident.

11-12-2012, 11:28 AM
I thought about regular expressions you know but when I was studying JavaScript a few years back, I kinda got stumped on that subject :p

I'll give it another read though if it will make life easier.

Thank you for your reply.

Kind regards,


11-12-2012, 11:38 AM
Just been looking through some regular expression tutorials and it's just far too complicated for me to understand.

Also your code doesn't work and I've been trying to modify it, but again I haven't got a clue what I'm doing.

I'll stick to manually checking the input because I don't just want to copy some code I don't understand even if it does what I want. I want to really know my code well and be able to change it.

Thanks anyway,