I would like to accept only small and capital letters from the user.
I tried the below code, it echoes the invalid character message but doesn\'t work. I mean it doe
There are several issues with the code, and the one you are stuck with is probably that you have the form and its processing in the same PHP file. That’s possible, but it requires a different approach. For a starter, it’s probably better to separate them.
What happens with the code posted is that the PHP processor tries to process the form data when no form has been submitted, without even checking for the presence of the data. Now $fname
is undefined, so the test always fails.
The test is wrong, too. Now it only checks whether $fname
contains at least one letter. For example, if(!preg_match ('/^[a-zA-Z]+$/', $fname))
would test that $fname
consists of one or more Ascii letters and nothing else.