PHP防止提交仅包含“空格"的空白字段.人物 [英] PHP Prevent the submission of empty fields containing only "space" characters

查看:119
本文介绍了PHP防止提交仅包含“空格"的空白字段.人物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用PHP时,如何验证简单的文本输入以检查该字段是否仅包含空格字符,我想防止它使用仅由空格组成的空白名称来登录我的网站对于其他输入(例如评论)?如果我检查它是否为空,则它将返回不为空,因为它无论如何都将空格视为字符.

When using PHP how can I validate a simple text input to check if the field only contains space character, I want to prevent it from being possible to sign up to my website with a blank name consisting of just spaces and the same goes for other inputs such as comments? If I check if it is empty it just returns it as not empty as it considers a space as a character regardless.

为了将来参考,答案是首先修剪我的帖子数据:

For future reference the answer was to first trim my post data:

$variable = mysql_real_escape_string(stripslashes(trim($_POST['field'])));

也可以在此处进行消毒,然后进行验证,然后可以使用empty():

Also sanitised it here and then as for validation, you can then just use empty():

if(empty($variable)) {
//do something
}

推荐答案

您可以trim发布的输入,并查看其输入的empty(如果显示)是否显示错误.

You can trim the posted input and see if its empty, if it is, display an error.

if(empty(trim($_POST['comments'])))
{
    // Its empty so throw a validation error
    echo 'Input is empty!'; 
}
else
{
    // Input has some text and is not empty.. process accordingly.. 
}

有关trim()的更多信息,可以在这里找到: http://php.net /manual/en/function.trim.php

More info on trim() can be found here: http://php.net/manual/en/function.trim.php

这篇关于PHP防止提交仅包含“空格"的空白字段.人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆