這是我使用的表單驗證腳本:如何驗證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(我的服務器環境)崩潰。
您應該解決您的服務器上使用正則表達式。 – SLaks 2010-06-21 17:47:35
重複: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
Tomcat肯定可以提供包含「$」字符的腳本。此外,很難驗證電子郵件地址的*語法*,以及*不可能*判斷它是否真實。 – Pointy 2010-06-21 17:51:28