2

I am using preg_match for restrict the special characters in form post. Now I need to restrict some special characters only like %,$,#,* and I need to post like . How to possible to restrict some special characters only.

My code:

<?php
$firstname='';
if(isset($_POST['submit']))
{
    $firstname=$_POST['firstname'];
    if(preg_match("/[^a-zA-Z0-9]+/", $firstname))
    {
    echo 'Invalid Name';
    }
    else
    {
    echo $firstname;
    }

}
?>

<html>
<body>
<form method="post">
<input type="text" name="firstname"/>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
Marcin Nabiałek
  • 98,126
  • 37
  • 219
  • 261
PNG
  • 243
  • 2
  • 6
  • 17
  • 1
    Maybe this will help you. http://stackoverflow.com/a/14114419/466082 – Arturs Aug 13 '14 at 08:33
  • I suggest allowing `-` as a character in firstnames.... haven't you every heard of `Jean-Claude Van Damme`? Though I can't think of any first names that contain numbers off the top of my head – Mark Baker Aug 13 '14 at 08:38

2 Answers2

3

Blacklisting (=enumerating invalid characters) is not an option in the unicode world. Consider for example, a "name" like this:

Ж☝ⓚƒ

You don't really want to blacklist all of these.

A whitelisting approach is, on the contrary, quite simple using the u mode and unicode properties:

var_dump(preg_match('/^[\p{L}\p{N}]+$/u', 'ßäßå'));  // 1
var_dump(preg_match('/^[\p{L}\p{N}]+$/u', 'r2d2'));  // 1
var_dump(preg_match('/^[\p{L}\p{N}]+$/u', 'w#t?'));  // 0
var_dump(preg_match('/^[\p{L}\p{N}]+$/u', 'Ж☝ⓚƒ'));  // 0

And since we're talking about validating real names, please read Falsehoods Programmers Believe About Names before you start complicating things.

georg
  • 195,833
  • 46
  • 263
  • 351
  • You could add some punctuations like `'` or `-` for `O'Connors` or `Jean-François` – Toto Aug 13 '14 at 09:10
  • @M42: the linked article is quite helpful and entertaining - read it! – georg Aug 13 '14 at 09:14
  • Sure, I've already read it, I just said it may have punctuation in name. – Toto Aug 13 '14 at 09:18
  • @M42: I understand. The thing is, if we start adding punctation, we should do this correctly (to disallow things like `O'O'Connor`) and then our assumptions about the structure will turn out wrong (they will do), and this is a never-ending story. There's no algorithm to validate every possible human name. – georg Aug 13 '14 at 09:23
2

You should use:

([%\$#\*]+)

to match those characters.

So in preg_match you should use:

if(preg_match("/([%\$#\*]+)/", $firstname))
{
   echo 'Invalid Name';
}
else
{
   echo $firstname;
}
β.εηοιτ.βε
  • 16,236
  • 11
  • 41
  • 53
Marcin Nabiałek
  • 98,126
  • 37
  • 219
  • 261