Sbml Sbml - 1 year ago 72
PHP Question

Php Sanitize and Validate form with some character exceptions

I'm using in Php Sanitize and Validate Filters but I have problems to add some rules, I have some basic knowledge of php so I think this question is easy for you.

if ($_POST['ccp_n'] != "") {
$ccp = filter_var($_POST['ccp_n'], FILTER_SANITIZE_NUMBER_INT);
if (!filter_var($ccp, FILTER_VALIDATE_INT)) {
$errors .= 'Insert a valid code.<br/>';
} else {
$errors .= 'Insert a code.<br/>';

I need to add a minimum and maximum number of characters (14-15) and I want to accept this characters ( - or space ) .The exact sequence is 0000-0000-0000 (the last four digits could be 5 too


Answer Source

You can use preg_match and apply a regular expression.

preg_match ( string $pattern , string $TestString) See here in detail

The pattern is the problem. You need to define in detail what is allowed.

For example, the pattern:


would be the whole string from start ^ to the end $. 4 digits, hyphen, 4 digits, hyphen, 4 to 5 digits.

See it here on Regexr


I added the D modifier to the end, otherwise the $ not only match to the end of the string, but also before a newline as last character in the string. See here for php modifiers in detail

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download