shakeit Posted March 4, 2008 Share Posted March 4, 2008 Hello everybody, I have a little issue regarding chars and signs that are aprooved in a text string. I want to check the field $name to contain only small and big letters which is usual in a name. But it happens that some people have two names, e.g. one main and one middle name. So I want to allow the user to write both names in one text field, i.e. by using space between them, for example: Jon Thomas. How do I add the space character in the code below in $goodChars, if it is possible? Thanx very much in advance and I hope you understand the problem description :-) ---------------------------------------------------------------------------------- public function validateName($name) { $goodChars = "^[a-z_A-Z]+$"; if( ereg("$goodChars",$name) && strlen($name) > $nameLengthMIN ) { return true; } else { return false; //Only A-E and - } } --------------------------------------------------------------------- Quote Link to comment Share on other sites More sharing options...
thebadbad Posted March 4, 2008 Share Posted March 4, 2008 You could simply add a space inside the square brackets. But I think you need to change it a bit, 'cause now a username could contain only an underscore (or more). Do you want an underscore to be allowed at all? If you want the name to either be pure a-zA-Z or a-zA-Z, then one underscore or space, and then a-zA-Z again, this regex would work (change ereg to eregi to make the check case insensitive): ^[a-z]+([ _][a-z]+)?$ Quote Link to comment Share on other sites More sharing options...
shakeit Posted March 4, 2008 Author Share Posted March 4, 2008 Hmm.. no i do not want the underscore in the name, that was my bad, i didnt even notice that, heheh... I only want, small letters a to z, big letters A to Z and space. so is this right then: ^[a-z]+([ ][A-Z]+)?$ Quote Link to comment Share on other sites More sharing options...
thebadbad Posted March 4, 2008 Share Posted March 4, 2008 Yep, as long as you use eregi. And it's a bit confusing to use both a-z and A-Z when the search is case insensitive. I would stick with lowercase letters (just for the looks - it won't change any functionality in the script). Quote Link to comment Share on other sites More sharing options...
shakeit Posted March 4, 2008 Author Share Posted March 4, 2008 Okey , so I can set all of the letter to lower case in the code AND change ereg to eregi and still use both small and big letters? Thanx man... Quote Link to comment Share on other sites More sharing options...
thebadbad Posted March 4, 2008 Share Posted March 4, 2008 Correct Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.