阻止来自表单的特定关键字搜索

时间:2014-01-30 17:50:36

标签: javascript php jquery search

我的页面上的搜索表单是

<form id="headbar-search" action="search.php" method="GET" x-webkit-speech="x-webkit-speech">
<input type="text" name="search" id="jsid-search-input" value="<?php echo$_GET['search']; ?>" class="ui-autocomplete-input search search_input" placeholder="Search&#8230;" tabindex="1"/>
<div class="ui-widget"></div>            
</form> 

我不希望用户能够搜索某些单词....

如果他们搜索“f ***”我希望它重定向到主页或一个简单的javascript通知,说不允许....谢谢!

1 个答案:

答案 0 :(得分:0)

要么在你的PHP脚本中捕获坏词,要么将你的php请求放在一些javascript和陷阱那里。例如....

HTML ...

<html>
    <head>
        <link rel="stylesheet" href="style.css"></link>
        <script src="functions.js"></script>
    </head>
    <body>
        <form id="headbar-search">
            <input type="text" id="search-box"/>
            <input type="submit" onclick="search();"></input>      
        </form>
    </body>
</html>

... JS

var badWords = ["gosh","darn"];
function search() {
    var searchStr = document.getElementById("search-box").value;
    var badWordHit = false;
    for (key in badWords) {
        if (badWords[key] == searchStr) {
            badWordHit = true;
        }
    }
    if (badWordHit) {
        alert("Oh no you didn't!");
    }   else    {
        // ajax request here
    }       
}