preg_match

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

preg_match

Post by michaelk46 »

I am trying to make sure that the only characters in a name field from an HTML form are letters and optionally spaces if the user is so inclined to use them. The script works if there are only letters, but the optional space is giving me some fits. I think I have the preg_match search options set incorrectly, but i haven't been able to find much on setting an optional parameter for it in google.

Here is the script

Code: Select all

$name = $_POST['name'];
	if (!preg_match("/^[a-zA-Z]+(\s)?$/", $name)) //verify $name contains only letters and spaces
		{
			echo htmlspecialchars('Name field is invalid, letters and spaces only please correct and re-submit.');
			exit();
		}

Anytime a name without a space is typed in, it works fine, but once a space is inserted such as a space between a first name and last name (IE...John Smith), it errors out and says the name field is invalid. Help....please
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: preg_match

Post by AbraCadaver »

A better one might be:

Code: Select all

"/[\w]+/"
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: preg_match

Post by michaelk46 »

Still get 'Name field is invalid, letters and spaces only please correct and re-submit.'

I was assuming that you meant that I should replace the \s with the \w
Here is the used search string i used after the replacement... "/^[a-zA-Z]+[\w]+?$/"
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: preg_match

Post by michaelk46 »

Nevermind... I got it... Thanks dude...
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: preg_match

Post by AbraCadaver »

michaelk46 wrote:Still get 'Name field is invalid, letters and spaces only please correct and re-submit.'

I was assuming that you meant that I should replace the \s with the \w
Here is the used search string i used after the replacement... "/^[a-zA-Z]+[\w]+?$/"
No, replace the entire pattern. That's why I used quotes and delimiters :wink:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: preg_match

Post by michaelk46 »

Yeah, I finally caught that once I looked a little closer... Thanks again...I appreciate the help.
Post Reply