简体   繁体   English

php表格。 表单验证。 骇客

[英]php form. form validation. name chacking

I have 1 problem here about validate name in HTML form using PHP. 我这里有1个关于使用PHP验证HTML格式名称的问题。 i have done all code but have 1 problem: 我已经完成了所有代码,但是有1个问题:

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 如果用户输入“”(空格),则必须返回错误消息,即名称不正确,并且用户必须使用az或AZ输入实名,因此,如果有人知道如何制作算法,或者它是内置函数请帮我

  1. Use trim() on the input to get rid of white space (eg the " ") 在输入上使用trim()消除空白(例如“”)
  2. Use strlen() to see if it's zero 使用strlen()查看它是否为零

So, let's say you get the user's input as a POST variable... eg $name = $_POST['name'] . 因此,假设您将用户输入作为POST变量...例如$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 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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