检测文本框是否不包含某些单词
Detect if a textbox does not contain certain words
我想知道如何检测文本框是否包含某些单词。例如,如果文本框不包含'who', 'what', 'why', 'when'或'where',则会运行某种函数。
JavaScript:function command() {
var srchVar = document.getElementById("srch");
var srch = srchVar.value;
var t = srch;
if (srch == '') {
alert('Please do not leave the field empty!');
}
else if (srch.indexOf('time') != -1) {
alert('The current time according to your computer is' + ShowTime(new Date()));
}
else if (srch.indexOf('old are you') != -1) {
alert("I am as old as you want me to be.");
}
else {
if (confirm('I am sorry but I do not understand that command. Would you like to search Google for that command?') == true) {
window.open('https://google.co.uk/#q=' + srch, '_blank');
}
else { /* Nothing */ }
}
}
HTML: <!DOCTYPE html>
<html>
<head>
<script src="script.js" type="text/javascript"></script>
<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<div class="ui-widget">
<input class="search-field" id="srch" onkeypress="searchKeyPress(event);" placeholder="ask me anything" spellcheck="false">
</div>
</body>
</html>
您将希望在按下按钮时调用该函数。尝试在HTML中添加如下内容:
<input class="info" onclick="contains();" type="button">
在你的JS
function contains() {
var srchVar = document.getElementById("srch");
var srch = srchVar.value;
if (
(srch.indexOf('who')==-1) &&
(srch.indexOf('what')==-1) &&
(srch.indexOf('why')==-1) &&
(srch.indexOf('when')==-1) &&
(srch.indexOf('where')==-1)
) {
alert("That is not a question");
}
}
您将来会想要将它与您的command()
函数合并。
还有,info();
做什么?
相关文章:
- 删除javascript中包含特定单词的行
- Javascript(如果var包含一个单词,则仅更改该单词的颜色)
- 忽略包含特定单词的模式
- Javascript 警报:如果消息包含某个单词,则阻止
- 如何检查我的字符串是否包含某些单词
- .CSS.防止文本中的行仅包含一个单词
- 如何选择不包含某个子元素的元素
- JavaScript正则表达式测试字符串是否包含特定单词(带变量)
- 如果单元格中的文本包含特定单词,则在其中放入图像
- 如何制作一个包含必需单词js+HTML的文本框
- 正则表达式到带有起始字母,它包含一个单词
- 更改包含指定单词的元素的样式
- 谷歌电子表格脚本为包含某个单词的行着色,并自动将该行移动到另一张工作表
- 删除包含特定单词的对象/字段
- 检查javascript中包含多少单词的字符串
- 遍历数组并删除包含特定单词的所有值
- 使用Gulp和Gulp替换名称克隆一个项目,但重命名多个文件夹中包含某个单词的文件
- 不包含特定单词的正则表达式
- 使用正则表达式获取包含特定单词的句子
- 为包含非单词字符的单词标记字符串