A
A
Andrew2016-01-06 02:24:37
PHP
Andrew, 2016-01-06 02:24:37

How to write regular expression validation for characters that should not be present?

Hello, already the brain refuses from fatigue, but it is necessary to add the validation of the fields. How to correctly check the field for the presence of prohibited characters in any part of the word? No matter what I tried, it doesn't work...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Nalomenko, 2016-01-06
@ntzch

Why is this regular? Use the native PHP function strpbrk()
As an example:

$illegal = "#$%^&*()+=-[]';,./{}|:<>?~";
$my_string = "Hello & World";
echo (false === strpbrk($my_string, $illegal)) ? 'Allowed' : "Disallowed";

Well, or, if you really want a regular, then like this:
return preg_match('/[#$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\\\]/', $string);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question