Special Characters ( - & ' " / \ % * # ) validation in a string

Hello,

I am developing a simple form to collect user information. I want a sample PHP code, that will validate the input string to display whether it has any of the below list of special character in the string.

Special Characters ( - & ’ " / \ % * # )

Can one of please help me. More detailed info below:

I have a form that submits the input to a PHP modules using Ajax Post. When my PHP modules reads the POST values, it should validate to check if the special characters are available, if it is available then display “No special characters allowed”

Anyones help will be so greatful

Thank you.
Vim

Try using PHP regular Expressions:
[php]

<?php $yourstring = "the string that you want to validate"; $pattern = '/[-&\'\"\\\/%\*#]/'; if (preg_match($pattern, $yourstring)){ echo "No special characters allowed" } else { // none of the special characters were found } ?>

[/php]

Note I didn’t actually test this specific regular expression in a PHP environment, but I did test it on Rubular (http://rubular.com/) and it worked.

Ah and there is a syntax error. There should be a semicolon after the line with echo

[php]
echo “No special characters allowed”;
[/php]

You can use the function he created for you, but there is already a function which handles this kind of cases.
The function is htmlspecialchars().

Here’s an example of using this function:
[php]
htmlspecialchars($yourstring);
[/php]

This function is supposted to illiminate hacks which are made using HTML and JS.

He doesn’t want to eliminate them. He wants to check if the user is using them or not.

Sponsor our Newsletter | Privacy Policy | Terms of Service