input输入框自动消除输入的空格 - 去除input文本框输入时的空格

博主:jhchinajhchina 2023-12-01 327 0条评论
摘要: 只要有空格都会自动消除,禁止前中后输入空格<input type="text" class="form-control&qu...

只要有空格都会自动消除,禁止前中后输入空格

<input type="text" class="form-control" id="assetId" onkeyup="this.value=this.value.replace(/[, ]/g,'')"></input>

onkeyup="this.value=this.value.replace(/[, ]/g,'')" 的作用:

① 在input框里面输入数据的过程中,出现空格,自动消除

② input首尾出现空格,自动消除

③ 复制粘贴的文本里面出现空格,自动消除

禁止前后输入空格,不禁止中间输入空格

<input type="text" name="test" onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"></input>
onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"


输入框自动清除首尾空格


在输入框加入onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')",前后禁止输入空格,输入空格自动清空,输入的时候清空空格

<input type="text" placeholder="输入查询" onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')">

 去字符串的首尾空格

var str = " abc ";
var str1 = str.trim();
console.log(str1); //结果为“abc"

 输入框可以输入空格,在鼠标点击框外后自动清除空格

function autoDeleteSpace(obj) {//[name=LastName]
    $(document).on('blur',obj, function () {
        $(this).val($(this).val().trim());
    })
}
autoDeleteSpace('input');