2011-07-05 39 views
0

使用javascript驗證和一些jquery在DOM中顯示變量錯誤消息,並突出顯示錶單標籤和字段。突出顯示多個輸入ID的JavaScript驗證

對於這個特定的錯誤信息,我需要突出顯示2個字段(email1和email2)。 不確定如何將它添加到我的當前設置爲以下警報:

customAlert ("email2",bnadd_msg_022); 

簡單的問題,如何我添加EMAIL1混進去?

編輯:

這裏是我的jQuery函數:

function customAlert(inputID,msg){ 

     $("li").removeClass("alertRed"); 
     $("input").removeClass("CO_form_alert"); 
     $("select").removeClass("CO_form_alert"); 
     var div = $(".errorPopup"); 
     div.css({"display":"block"}); 
     $("#"+inputID).addClass("CO_form_alert").parent().addClass("alertRed"); 
     if (div.length == 0) { 
     div = $("<div class='errorPopup' onclick='$(this).hide();'></div>"); 
     $("body").prepend(div); 
     } 
     div.html(msg); 
     $("#"+inputID).focus(function(){ 
     $(this).unbind('focus'); // remove this handler 
     $('.errorPopup').hide(); // hide error popup 
    }); 

所以,在這種情況下,和另外一個,我需要強調同時

+4

這將取決於什麼 「customAlert()」 函數的模樣。 – Pointy

+0

如果你可以發佈簡單的HTML和JS,你會得到更好的答案 –

+0

@Pointy @Bobby Borszich - 見上面的編輯。謝謝。 – Jason

回答

1

我想在Javascript中使用參數'數組',並簡單地將customAlert定義爲一個沒有參數的函數。

function customAlert(){ 
    var args = arguments; 
    if(args.length > 1) { 
     // check that custom alert was called with at least two arguments 
     var msg = args[0]; 
     $("li").removeClass("alertRed"); 
     $("input").removeClass("CO_form_alert"); 
     $("select").removeClass("CO_form_alert"); 
     var div = $(".errorPopup"); 
     div.css({"display":"block"}); 
     if (div.length == 0) { 
      div = $("<div class='errorPopup' onclick='$(this).hide();'></div>"); 
      $("body").prepend(div); 
     } 
     div.html(msg); 
     for(var i = 1; i < args.length; i++) { 
      var inputID = args[i]; 
      $("#"+inputID).addClass("CO_form_alert").parent().addClass("alertRed"); 
      $("#"+inputID).focus(function(){ 
       $(this).unbind('focus'); // remove this handler 
       $('.errorPopup').hide(); // hide error popup 
      }); 
     } 
    } 
} 

然後調用它像這樣:

customAlert(bnadd_msg_022,"email2"); // note the order of the arguments has changed 
customAlert(bnadd_msg_022,"email1","email2"); 
+0

不幸的是,通過上面的代碼,標籤和表單字段的突出顯示不再存在 - 特別是addClass - CO_form_alert和alertRed – Jason

+1

@Jason我錯過了代碼中的那一行 - 我將更新答案以將其包含在for循環中。 –

+0

美麗。像魅力一樣工作。我很感激幫助。我正在更加徹底地學習jquery和javascript(我知道的大部分內容都涉及到前端開發,視覺效果等),所以我很欣賞這些知識 – Jason

0

2場在沒有看到代碼,我猜測是你可以改變customAlert函數來獲取可選的第三個參數。

我會這樣做的方式是讓第三個參數可以接受n個字段ID的數組。在customAlert函數中,我會檢查是否傳遞參數,如果是,則遍歷數組並突出顯示其中包含的任何標識。

+0

見編輯。謝謝。 – Jason

+0

這可能會起作用,假設單個msg參數就足夠了。 – picus