Allow only capital and small letters

前端 未结 5 590
暗喜
暗喜 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.

    0 讨论(0)
  • 2021-01-18 08:16
    if(!isset($_POST['fname']) || !ctype_alpha($_POST['fname'])){
      // can i haz alpha letters only?
    }
    

    (reference)

    0 讨论(0)
  • 2021-01-18 08:28

    The general idea of checking for characters that don't match the [a-zA-Z] pattern is a good one.

    However, the "not" part of your if condition is in the wrong place if you want this to work. What you've got now just makes sure that any single character in fname is an upper- or lower-case Latin letter.

    You want to push the "not" part of the logic into the pattern:

    if (preg_match('/[^a-zA-Z]/', $fname)) {
    

    This checks if any character in fname is not a Latin letter, which is what you're trying to do.

    Edit: Your new update has a different test that also works (it appears to be from sourcecode's updated answer, but you've got several tests from the different answers here that will work equally well). But, your updated post makes it clear that your problem isn't really with the pattern for testing the name.

    Your code looks like this:

    if (/* invalid fname */) {
        echo "Invalid characters";
    }
    
    if (/* empty fname */) {
        echo  '<span> First name is required</span>';
    }
    
    else {
        /* insert into database */
    }
    

    That else clause only depends on the the if that comes immediately before it: the check whether fname is empty. In other words, regardless of the result of your check against the characters of fname, you insert it into the database whenever it's not empty.

    One easy way to fix this is to just change your second if to an elseif. This will chain all three conditionals together, so the final else block will only occur if both of the earlier conditionals that print error messages weren't triggered.

    if (/* empty fname */) {
        echo  'First name is required.';
    }
    
    elseif (/* invalid fname */) {
        echo 'Invalid characters';
    }
    
    else {
        /* insert into database */
    }
    
    0 讨论(0)
  • 2021-01-18 08:32

    If you just want to check you could use ctype_alpha() but you said you want to ACCEPT only letters so if you choose to accept the input you could:

    $fname=preg_replace('/[^a-z]/i','',$fname);
    

    better after the check

    0 讨论(0)
  • 2021-01-18 08:33

    use this , this is giving me correct answer

    if(!preg_match ('/^([a-zA-Z]+)$/', $fname)){
        echo "Invalid characters";
    }
    else{
        echo "correct";
    }
    
    0 讨论(0)
提交回复
热议问题