Allow only capital and small letters

前端 未结 5 688
暗喜
暗喜 2021-01-18 07:59

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

5条回答
  •  失恋的感觉
    2021-01-18 08:08

    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.

提交回复
热议问题