[英]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');
}
});
這是樣本小提琴: https : //jsfiddle.net/2qorhekd
測試字符串是否包含http,https或www的函數
var valid = function(text){
var regex = /(https?|www)/;
return !regex.test(text);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.