简体   繁体   中英

PHP Form Validation Regular Expression, no symbols or numbers

Ok I am trying to get the users First Name

the form gets their name perfectly fine and posts it into a variable.

Now I am trying to do error checking

else if(!preg_match("/^[\w-]+$/", $firstNameSignup)) {
       $firstNameSignupError = "Your first name cannot contain numbers or symbols, you entered " . $firstNameSignup;
       $firstNameSignup = "";
      }

I tried the above code and it does not like me but my if statement

if(!isset($firstNameSignup) || $firstNameSignup == "") {
   $firstNameSignupError = "You must enter your first name";
  }

works fine so I know that the error is in that else if statement... most likely in my regular expression

any help??? I'm totally at a loss (really new to PHP and regular expressions)

Thanks Shelby

else if(preg_match('#[^a-z]+$#i', $firstNameSignup)) {
            $firstNameSignupError = "Your first name cannot contain numbers or symbols, you entered " . $firstNameSignup;
            $firstNameSignup = "";
        }

finally found at that the above code worked

Preg match returns FALSE on error.

You need to do like so;

else if(preg_match("/^[\\w-]+$/", $firstNameSignup) == 0) { // no matches

http://au2.php.net/preg_match

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM