从Javascript文本框中删除制表符空间

Removing tab space from text box in Javascript

本文关键字:删除 制表符 空间 Javascript 文本      更新时间:2023-09-26

如何从文本框中删除制表符空格值?我的功能代码是::

function validTitle() {
if (window.document.all.dDocTitle.value == "") {
alert("Please enter the Title");
window.document.all.dDocTitle.focus();
return false;
} 
return true;
}

我想再添加一个条件,用于在文本框中删除与window.document.all.dDocTitle.value捕获值的制表符空间

可以使用String.trim() function ():

function validTitle() {
  // just a remark: use document.getElementById('textbox_id') instead, it's more supported
  var textBox = window.document.all.dDocTitle; 
  if (!(typeof textBox.value  === 'string') || !textBox.value.trim()) { // if textbox contains only whitespaces
    alert("Please enter the Title");
    textBox.focus();
    return false;
  } 
  // remove all tab spaces in the text box
  textBox.value = textBox.value.replace(/'t+/g,'');
  return true;
}