javascript 如何开始文字输入?[已关闭]

hyrbngr7  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(141)

13小时前关门了。
Improve this question
当我从用户输入,我想让用户明白,他们应该键入的东西开始与"TR"。这就是为什么我想要一个输入像下面的图像。如果有人粘贴或键入的东西开始与"TR",我想防止他们不要"TRTR"。
我的意思不是"占位符"。有没有办法做到这一点?你可以给建议使用输入id选择器。
enter image description here

$("#IBAN").val("TR"); document.getElementById('IBAN').addEventListener('input', function(){ $("#IBAN").bind("paste", function(e){    var iban = $('#IBAN').val();      // access the clipboard using the api     var pastedData = e.originalEvent.clipboardData.getData('text');   if (this.value.length >=
svmlkihl

svmlkihl1#

这应该可以让你开始。当输入字段至少有2个字符并且不是以"TR"开头,或者以"TRTR"开头时,它会显示一个警告。

document.getElementById('test').addEventListener('input', function(){
    if (this.value.length >= 2 && !this.value.match(/^TR/i)) {
        alert('Must start with TR')
    }
    if (this.value.match(/^TRTR/i)) {
        alert('Must not start with TRTR')
    }
})
<input id="test" value="TR">

相关问题