2015-09-06 68 views
0

如何从文本框中删除选项卡空间值。我的功能代码::从Javascript中的文本框中删除选项卡空间

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

我想通过添加1更多的条件在文本框中与window.document.all.dDocTitle.value

捕获值中去除标签空间

回答

1

你可以这样做String.trim()功能():

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; 
} 
+0

谢谢。我做到了,但是在修剪的情况下仍然没有提醒。为了测试,我复制了一个标签空间标题从记事本粘贴到标题字段,但它没有给出警报。总之,我想限制用户从任何来源输入标签空间标题,因为当这个元数据在第三个应用程序中流动时它会产生问题 –

+0

我这样做了,它也不能工作......... function validTitle( ){ \t if(window.document.all.dDocTitle.value ==“”){ \t alert(“请输入标题”); \t window.document.all.dDocTitle.focus(); \t return false; \t} \t window.document.all.dDocTitle.value = value.replace(/(^ [\ t] + | [\ t] + $)/ g,''); \t返回true; \t} –

+0

对不起!我误解了你的需求。我刚编辑我的解决方案。我认为这是你需要的。 –

相关问题