如果在文本区域中找到 (http)、(https) 或 (www),如何禁用提交按钮

How to disable submit button if (http), (https), or (www) are found in textarea

我已经在 javascript 中创建了一个代码,如果选择了下拉选项,它会禁用表单上的提交按钮,但如果字符串 (http),我也想关闭提交按钮,( https) 或 (www) 被输入到表单的文本区域中。在正确方向上的任何帮助都会很棒。

您可以侦听 keyup 事件并在事件触发时检查输入。

类似于:

let illegalWords = ["http", "https", "www"];

function checkText(text) {
  if (text.length >= 1) {
    for (let word of illegalWords) {
      if (text == word) {
        // word found, remove submit button
        console.log("found");
        document.getElementById("submit").style.display = "none";
      }
    }
  }
}
<textarea onkeyup='checkText(this.value);'></textarea>
<button type="submit" id="submit">Submit</button>

您可以向此代码添加一个 else 块,以便在删除非法单词时显示提交按钮(如果当前不可见)。

试试这个

<textarea id="my-textarea"></textarea>

document.querySelector('#my-textarea').addEventListener('input', (event) => {
    if (event.target.value.includes('http') || event.target.value.includes('https') || event.target.value.includes('www')) {
    console.log('invalid');
  } else {
    console.log('valid');
  }
});

这是示例 fiddle:https://jsfiddle.net/2qorhekd

测试字符串是否包含 http、https 或 www 的函数

var valid = function(text){
  var regex = /(https?|www)/;
  return !regex.test(text);
}