2010-06-21 67 views
1

這是我使用的表單驗證腳本:如何驗證JavaScript中的電子郵件字段?

<script language="JavaScript"> 

function formCheck(formobj){ 
    var fieldRequired = Array("Name", "Email", "Phone", "comments"); 
    var fieldDescription = Array("Name", "Email", "Phone", "Comments"); 
    var alertMsg = "Please complete the following fields:\n"; 

    var l_Msg = alertMsg.length; 

    for (var i = 0; i < fieldRequired.length; i++){ 
     var obj = formobj.elements[fieldRequired[i]]; 
     if (obj){ 
      switch(obj.type){ 
      case "select-one": 
       if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      case "select-multiple": 
       if (obj.selectedIndex == -1){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      case "text": 
      case "textarea": 
       if (obj.value == "" || obj.value == null){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      default: 
      } 
      if (obj.type == undefined){ 
       var blnchecked = false; 
       for (var j = 0; j < obj.length; j++){ 
        if (obj[j].checked){ 
         blnchecked = true; 
        } 
       } 
       if (!blnchecked){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
      } 
     } 
    } 

    if (alertMsg.length == l_Msg){ 
     return true; 
    }else{ 
     alert(alertMsg); 
     return false; 
    } 
} 
// --> 
</script> 


<form onsubmit="return formCheck(this);" action="/capture.weblead" method="post"> 
First Name: <input type=text name="FirstName" size="25"><br> 
Last Name: <input type=text name="LastName" size="25"><br> 
<input type=submit value="Submit Form"> 
</form> 

這只是它不用於驗證真實的電子郵件地址的偉大工程。如何改變這種形式,以便它呢?

該腳本不能包含任何美元符號,否則Tomcat(我的服務器環境)崩潰。

+10

您應該解決您的服務器上使用正則表達式。 – SLaks 2010-06-21 17:47:35

+1

重複:http://stackoverflow.com/questions/46155/validate-email-address-in-javascript http://stackoverflow.com/questions/2783672/email-validation-javascript http://stackoverflow.com/questions/940577/javascript-regular-expression-email-validation – 2010-06-21 17:49:03

+1

Tomcat肯定可以提供包含「$」字符的腳本。此外,很難驗證電子郵件地址的*語法*,以及*不可能*判斷它是否真實。 – Pointy 2010-06-21 17:51:28

回答

0

這是一個快速和骯髒的解決方案。修改emailRegexp變量的值以滿足您的需求。這裏已經有一些例子。正如波蒂指出的那樣,假陽性通常比假陰性通常要好。

function formCheck(formobj){ 
    var fieldRequired = Array("Name", "Email", "Phone", "comments"); 
    var fieldDescription = Array("Name", "Email", "Phone", "Comments"); 
    var alertMsg = "Please complete the following fields:\n"; 
    var emailRegexp =/@/; 

    var l_Msg = alertMsg.length; 

    for (var i = 0; i < fieldRequired.length; i++){ 
     var obj = formobj.elements[fieldRequired[i]]; 
     if (obj){ 
      switch(obj.type){ 
      case "select-one": 
       if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      case "select-multiple": 
       if (obj.selectedIndex == -1){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      case "text": 
      case "textarea": 
       if (obj.value == "" || 
        obj.value == null || 
        (fieldRequired[i] == "Email" && !obj.value.match(emailRegexp))) 
       { 

        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 


       break; 
      default: 
      } 
      if (obj.type == undefined){ 
       var blnchecked = false; 
       for (var j = 0; j < obj.length; j++){ 
        if (obj[j].checked){ 
         blnchecked = true; 
        } 
       } 
       if (!blnchecked){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
      } 
     } 
    } 

    if (alertMsg.length == l_Msg){ 
     return true; 
    }else{ 
     alert(alertMsg); 
     return false; 
    } 
}