多个电子邮件验证

时间:2013-04-02 06:27:33

标签: javascript validation

我有一个电子邮件ID字段,其中必须以逗号分隔多个电子邮件ID。如何在jsp页面中使用JavaScript执行所有电子邮件ID的验证?

1 个答案:

答案 0 :(得分:0)

可能会帮助您使用javascript验证多个电子邮件ID

 <html>
<title>email validation</title>
<script language="JavaScript" type="text/javascript">
    // Validate Email .
    function validateEmail(field) {
        var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}$/;
       return (regex.test(field)) ? true : false;
    }
    function validateMultipleEmailsCommaSeparated(emailcntl) {
        var seperator = ';';

        var value = emailcntl;

        if (value != '') {
            var result = value.split(seperator);

            for (var i = 0; i < result.length; i++) {

                if (result[i] != '') {

                    if (!validateEmail(result[i])) {
                        alert('Please check, `' + result[i] + '` email addresses not valid!');
                        return false;
                    }
                }
            }
        }
        return true;
    }
</script>
<table>
    <tr>
        <td>
            <input type="text" id="email" name="email" />
            <input type="button" onclick="validateMultipleEmailsCommaSeparated(email.value);"
                value="check Email" /><br />
            <div id="errorDiv" />
        </td>
    </tr>
</table>
</html>