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!
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.
$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
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.
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 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
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.