单击提交时验证文本区域

Validate textarea when clicking submit

本文关键字:文本 区域 验证 提交 单击      更新时间:2023-09-26

我正在开发一个允许用户向系统发送反馈的网站。我已经通过使用文本区域和提交按钮创建了反馈表单。最重要的是,当用户点击提交时,如果用户输入了一些我不想让他们输入的单词,反馈就不会发送到系统;它将提醒用户在cicking-submit之前退出该单词。

从现在起,我只创建了一个简单的代码,如果用户输入了我不想让他们以反馈形式输入的单词,它将返回一些警告

这是我的代码

<form action="main.php" method="post">
    <textarea cols='10' rows='5' name='text'></textarea>
    <br/>
    <input type='submit' name='add' Value='Add to list' />
</form>
<?php
if (isset($_POST['add'])) {
    $banned = array('dog', 'cat', 'cow'); // Add more
    $entry = $_POST['add'];
    foreach($banned as $word): if (strpos($entry, $word) !== false) die('Contains banned word');
    endforeach;
}
?> 

这不是工作。有人能帮我解决这个问题吗?

提前感谢。

像一样尝试

$entry = $_POST['text'];    // Not $_POST['add'];
foreach ($banned as $word):
    if (strpos($entry, $word) !== false) {
        echo 'Contains banned word';
        exit;
    }
endforeach;

您的$entry将是文本框值,即$_POST['text']not$_POST[add']

USE in_array函数http://php.net/manual/ru/function.in-array.php

$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
<?php
if(isset($_POST['add'])){
    $banned = array('dog', 'cat', 'cow'); // Add more
    $entry = $_POST['text'];
    if(in_array($entry,$banned))
    {
        die('Contains banned word');
    }
}
?>

您想要进行单词审查。要完成此操作,请使用str_ireplace()函数。我在下面给出一个示例代码。这个函数的作用是用另一个词替换这个词。

<?php
$find=array('galaxy', 'planet', 'moon');
$replace=array('spiral', 'earth', 'satelite');
if (isset($_POST['userinput'])&&!empty($_POST['userinput']))
{
$user=$_POST['userinput'];
$user_new=str_ireplace($find, $replace, $user);
echo $user_new;
}
?>
<hr>
<form action="index.php" method="POST">
<textarea cols="50" rows="10" name="userinput"><?php echo $user; ?></textarea><br />
<input type="submit" value="submit" />
</form>

我建议您在客户端使用Javascript,只有在通过验证后才能提交代码,而不是每次输入文本时都将代码提交给服务器。

HTML

<form action="main.php" method="post">
<input type="text" onchange="validate_function(this)">
 //works with text area too.
<input type='submit' name='add' Value='Add to list' />
</form>

Javascript

validate_function(a)
{
r=0;
banned = array('dog', 'cat', 'cow');
var lines = a.val().split(''n');
for(var x=0;x<banned.length;x++) 
{    
for(var i = 0;i < lines.length;i++)
{
if (lines[i].indexOf(banned[x]) > -1)
 {
      r=1;
      break;
}
 else 
{
}
}
if(r==1)
break;
}
if( r==0)
return true;
else 
return false;
}

如果用户有任何禁用词,您所需要做的就是停止表单提交

<form action="main.php" method="post">
<textarea cols='10' rows='5' name='text' id='text'></textarea>
<br/>
<input type='submit' name='add' Value='Add to list' onclick='validate()' />
</form>

在您的javascript 中

<script>
function validate() {
   var text = document.getElementById('text').value;
   var banned = new Array('dog', 'cat', 'cow');
   for (var x in banned) {
      if ( strpos(text,banned[x],0) ) {
            continue; // will stop the loop
            return false; // will stop everything and will not let the form post to php
      }
   }
   return true; // will return true and will post the form to php
}
// Custom Function
function strpos (haystack, needle, offset) { // http://phpjs.org/functions/strpos/
  var i = (haystack + '').indexOf(needle, (offset || 0));
  return i === -1 ? false : i;
}
</script>

我希望这将有助于

PS:函数strpos取自phpjs.org