简体   繁体   English

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

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

I already have a code I created in javascript that disables the submit button on a form if a dropdown choice is chosen but I also want to turn off the submit button if the string (http), (https), or (www) is typed into textarea of the form. 我已经有一个在javascript中创建的代码,如果选择了下拉菜单,该代码将禁用表单上的提交按钮,但是如果键入了字符串(http),(https)或(www),我也想关闭提交按钮形式的文本区域。 Any help in the right direction would be awesome. 在正确方向上的任何帮助都将非常棒。

You could listen for the keyup event and check the input when the event fires. 您可以侦听keyup事件并在事件触发时检查输入。

Something like: 就像是:

 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> 

You could add an else block to this code to make the submit button appear (if not currently visible) when the illegal words are removed. 您可以在此代码中添加else块,以在删除非法单词时使“提交”按钮出现(如果当前不可见)。

Try this 尝试这个

<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');
  }
});

Here is the sample fiddle: https://jsfiddle.net/2qorhekd 这是样本小提琴: https : //jsfiddle.net/2qorhekd

Function to test if string has contains http, https or www 测试字符串是否包含http,https或www的函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM