Search code examples
phpstringvalidationpreg-match

validating only selected characters in string php (phone number validation)


i have a string containing alpha numeric characters. The script should return (echo) "true" if the string contains only 0-9, - , + , or the word NA (NA should be validated only if it contain no other characters and should echo false if the string contain any other character along with "NA"), The script should echo "false" if the string contains any other characters other than the specified characters.. How can i make this possible??

Thanks in advance.. :)

blasteralfred


Solution

  • if(preg_match('/^(NA|[0-9+-]+)$/',$str)) {
            echo "true\n";
    } else {
            echo "false\n";
    }