user1080247 user1080247 - 1 year ago 65
PHP Question

how to validate numbers and special characters only with regex in php

i have function to validate user input a against numbers-special chars pattern (no alpha)but i have not do it exactly,and i searched over the internet to find such this but i not find,any help

function validate_num($input){

return ( ! preg_match("^[0-9*#+]+$", $input)) ? FALSE : TRUE;


Answer Source

The syntax of your RegEx is not right. You need to add / before and after:

preg_match("/^[0-9*#+]+$/", $input))

Your RegEx seems right:


The example from the manual looks like this:

$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download