我在某些表单字段上使用文本编辑器,TinyMCE。它工作得很好。
但是,TinyMCE编辑器为表单中的每个字段返回一个HTML主体标记。如果用户完成了表单字段,这是非常好的,即;
'description' => string '<!DOCTYPE html>
<html>
<head>
</head>
<body>
a users response
</body>
</html>'
但是,如果用户没有完成字段,TinyMCE仍然返回一个包含空html主体的字符串,即下面的代码:
'description' => string '<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>'
我想做的是测试这个返回的"表单字段"是否只包含空的HTML字段,即HTML字段的主体内没有值。
总之,我如何测试上面的代码只包含空的正文标签?
这不是一个非常优雅的解决方案,但它工作。我只是使用strip_tag()从文档中删除标记,然后测试变量是否为空。如果它不是空的,我提交post变量。
if (isset($_POST['description']))
{
$preValidated = dsf_db_prepare_input($_POST['description']);
$testValue = trim(strip_tags($preValidated));
if(!strlen($testValue) >= 1)
{
$description ='';
}
else
{
$description ="$preValidated";
}
}
你可以在客户端使用jquery
if($('<html><body></body></html>').find('body').children().length < 1) {
console.log('empty')
} else {
console.log('not empty');
}
您可以验证并向服务器发送适当的响应