2013-08-21 46 views
0

我正在爲我的JavaScript類做一個表單,並且我被卡住了它的某個部分。我有一個單獨的驗證器JavaScript文件,並調用該HTML文件的功能。所有的驗證工作,如果表單區域未填寫。我想要做的是,如果字段留空,他們將無法驗證,並將值插入該字段。下面是表單字段的示例,html頁面中的javascript函數以及外部驗證器js文件。在HTML頭驗證後的Javascript填充表格

通話功能:

function formvalidation(thisform) { 
with (thisform) { 
if (textbox_validation(first_name,"Please enter your first name.")==false) 
{first_name.blur(); return false;}; 
if (textbox_validation(business_name,"Please enter your business. Please enter N/A if 
you do not have one.")==false) { business_name.focus(); return false; 
business_name.value=="N/A";}; 

外部JS驗證:

function textbox_validation(entered, alertbox) { 
with (entered) { 
if (value==null || value=="") { 
    alert(alertbox); 
    return false; 
} 
else { 
    return true; 
    } 
    } 
} 

因此驗證工作,重點是空字段,但是對於我的一些領域我他們想如果驗證失敗或者如果它沒有填充int,則填充一定的值。 business_name行代碼是當我試圖使其工作。任何幫助深表感謝!

+0

通常不鼓勵使用'with'。資料來源:http://www.yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/ – Halcyon

回答

0

Ordinarilly,你不會使用警報,而是會放的錯誤信息,spandiv要麼靠近input或在form的頂部(或底部)。另外(如@Frits van Campen提到的)它是generally bad practice to use with 嘗試這樣代替:

function textbox_validation(entered, errormsg) { 
    var errbox = document.getElementById(entered.id + '-errors'); // just to prevent writing it twice 
    // Note this requires the input to have an id, and the errer box's id to be the same with an '-errors' suffix. 

    // Instead of using with, just acces properties normally 
    if (!entered.value) { // The `!` "neggation" operater makes "falsy" values `true` 
         // "falsy" values include `false`, the empty string, `0`, `null`, `undefined`, `NaN` and a few others 
     // Put the error message in the DOM instead of alerting it 
     errbox.innerHTML = errormsg; 
     return false; 
    } 
    else { 
     // Wipe any previous error messages 
     errbox.innerHTML = ''; 
     return true; 
    } 
} 

而對於形式驗證,再一次;我們不要使用with。而且,試圖assing「N/A」的值時,你使用的比較運營商,而不是分配運營商,並且你做到了返回後:

function formvalidation(thisform) { 
    // just use the `!` "negation" operator 
    if (!textbox_validation(thisform.first_name, 
     "Please enter your first name.")) 
    { 
     thisform.first_name.blur(); 
     return false; 
    } 
    if (!textbox_validation(business_name, 
     "Please enter your business. Please enter N/A if you do not have one.")) 
    { 
     thisform.business_name.focus(); 
     thisform.business_name.value = "N/A"; // for assignment, use `=`. `==` and `===` are used for comparison 
     return false; // a return statement ends the function, make sure it's after anything you want to execute! 
    } 
} 
+0

我剛剛注意到你的帖子,我剛剛閱讀了有關不使用。我會用它來代替。我如何爲每個輸入創建並顯示不同的錯誤消息? – user2532765

+0

@ user2532765創建錯誤消息只是將它傳遞給textbox_validate(就像你已經在做)。要創建_error消息box_'HTML',請嘗試類似於''請注意類似的'id'和'getElementById(entered.id +'-errors')'顯然你會改變_'foo'_到你已經用於每個輸入的'id'。 – MattSturgeon

+0

@ user2532765此外,如果您使用這種隨意將其標記爲您接受的答案;) – MattSturgeon

0

使用DOM設置字段的佔位符。喜歡這個。

var myInput = document.getElementById('input1'); 
myInput.placeholder = 'This validation has failed.'; 
+0

感謝您的快速響應。我究竟在哪裏將該代碼放入HTML或外部JS文件中?而input1將是我的領域id像first_name? – user2532765

+0

把它放在alert(alertBox);'後面的外部js文件中。是的,input1是指您要爲其顯示的輸入字段設置的ID。請記住,每個輸入字段必須有不同的ID。 – Claude

+0

謝謝!你的救星! – user2532765