2010-07-07 118 views
0

我無法弄清楚什麼是缺少這樣當電子郵件是有效的,而跳過最後一個無效的消息,並移動到下一個項目的形式進行驗證:的Javascript電子郵件驗證

enter code here 
    if (document.form1.email.value.length > 0) { 

    var tst = document.form1.email.value; 
    var okd = ['bankofamerica.com','baml.com','magner.com','ml.com','ust.com','ustrust.com'] 
    for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() } 

    var emailRE = /^[a-zA-Z0-9._+-][email protected]([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/ 
    var aLst = emailRE.exec(tst) 
    if (!aLst) { 
     alert(tst + ' is not a valid e-mail') 
    } else { 
     var sLst = aLst[1].toLowerCase() 
     for (var i = 0; i < okd.length; i++) { 
      if (sLst == okd[i]) { 
       // alert(aLst[1] + ' is allowed');--> 

      } 
     } 

      if (i == okd.length) alert(aLst[1] + ' is not allowed. Please enter an email address with an authorized domain.') 

      document.form1.email.select(); 
      return false; 


    } 
} 

回答

0

我建議將此代碼放入一個函數中,可能名爲ValidateEmail()

在您的循環中:如果您確定電子郵件有效,請致電return true;。這將阻止進一步的執行。如果該域不匹配,讓它繼續循環檢查其他域。

如果循環完成而沒有返回true,那麼您將知道它最後不匹配return false;

編輯:使用try/catch語句來代替:

if (document.form1.email.value.length > 0) { 

    var tst = document.form1.email.value; 
    var okd = ['bankofamerica.com','baml.com','magner.com','ml.com','ust.com','ustrust.com'] 
    for (var i = 0; i < okd.length; i++) { okd[i] == okd[i].toLowerCase() } 

    try { 
     var emailRE = /^[a-zA-Z0-9._+-][email protected]([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/ 
     var aLst = emailRE.exec(tst) 

     if (!aLst) 
      throw (tst + ' is not a valid e-mail'); 

     // isValidDomain will be changed to 'true' only if it matches an item in the array 
     var isValidDomain = false; 

     var sLst = aLst[1].toLowerCase() 
     for (var i = 0; i < okd.length; i++) { 
      if (sLst == okd[i]) { 
       isValidDomain = true; 
       // We break here because a match has been found - no need to compare against the other domain names. 
       break; 
      } 
     } 

     if(!isValidDomain) 
      throw (aLst[1] + ' is not allowed. Please enter an email address with an authorized domain.'); 

     // If execution reaches here, you know it passed both tests! 
     return true; 

    } 
    catch(err) { 

     // This code block runs whenever an error occurs 

     alert(err); 
     document.form1.email.select(); 
     return false; 
    } 
} 

throw基本上就像一個goto命令。它將直接跳轉到代碼的catch(err)部分。大約嘗試,catch和throw

更多信息:

+0

此外,在你的第一個'if'語句,你可以'返回false;'是電子郵件絕對無效。這將阻止檢查每個域。 – 2010-07-07 19:01:18

+0

不工作,顯然我不知道該怎麼做... 必須有GoTo命令或其他東西才能讓它跳到最後,如果已經是真的......爲什麼這麼難? – miweiser 2010-07-07 19:52:31

+0

我不需要循環或休息,我只需要它進入下一步並跳過最後的 – miweiser 2010-07-07 20:14:54

0

非常感謝您科林!

,我不得不刪除以下兩行,以避免在運行到下一個驗證領域阻止代碼:

   isValidDomain = true; 
        // We break here because a match has been found - no need to compare against the other domain names. 
        // break - exits code from running on down to next item on page 
       } 
      } 

      if (!isValidDomain) 
       throw (aLst[1] + ' is not allowed. Please enter an email address with an authorized domain.'); 

      // If execution reaches here, you know it passed both tests! 
     // return true; - was not needed, stops code from running on page 

     } 
     catch (err) { 
+0

甜,我很高興你有工作:) – 2010-07-07 21:24:58