简体   繁体   中英

php form. form validation. name chacking

I have 1 problem here about validate name in HTML form using PHP. i have done all code but have 1 problem:

If user input " " (space) it mmust return error message that it is incorect name and user must inpput real name using az or AZ, so if someone have an idea how to make an alghorithm or it is a built-in function for this please help me

  1. Use trim() on the input to get rid of white space (eg the " ")
  2. Use strlen() to see if it's zero

So, let's say you get the user's input as a POST variable... eg $name = $_POST['name'] . Try this:

if(strlen(trim($name)) === 0) { // do stuff to tell user their name is invalid }

A better idea Use a regex

$legal_username_regex = "/^[A-Za-z0-9]+(?:[._-][A-Za-z0-9]+)*$/";

if (preg_match($legal_username_regex, $new_username)) { // do stuff for a valid username } else { // do stuff for an invalid username }

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