Jump to content

allowing space in textfield


shakeit

Recommended Posts

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 -

}

 

}

---------------------------------------------------------------------

Link to comment
https://forums.phpfreaks.com/topic/94235-allowing-space-in-textfield/
Share on other sites

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]+)?$

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.