如何在javascript中验证电子邮件字段?

时间:2010-06-21 17:46:05

标签: javascript validation

这是我用于表单验证的脚本:

<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(我的服务器环境)崩溃。

3 个答案:

答案 0 :(得分:14)

您可以在javascript中使用正则表达式

function is_email(email){      
var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailReg.test(email); } 

http://richwd.com/email-javascript

答案 1 :(得分:1)

我建议你使用jQuery和Validation插件:

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

它灵活,可靠且易于使用。

答案 2 :(得分:0)

这是一个快速而肮脏的解决方案。修改emailRegexp变量的值以满足您的需要。这里已经有一些例子了。正如Pointy指出的那样,假阳性通常比假阴性更好。

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;
    }
}