2015-09-06 67 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

對不起!我誤解了你的需求。我剛編輯我的解決方案。我認爲這是你需要的。 –

相關問題