格式&从textarea验证多个电子邮件地址

时间:2011-12-29 17:51:27

标签: javascript

我需要修剪空白区域并用分号分隔。完成后,在我传入数据库之前验证电子邮件地址是否采用正确的电子邮件格式。像这样:

“tom@yahoo.com; bob@yahoo.com”

enter image description here

2 个答案:

答案 0 :(得分:2)

这是我在JavaScript中使用jQuery Validate:

$.validator.addMethod('emailList', function(value, element) {
    if (this.optional(element)) return true;

    var flag = true;
    var addresses = value.replace(/\s/g,'').split(',');
    for (i = 0; i < addresses.length; i++) {
        flag = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(addresses[i]);
    }
    return flag;
}, '');

如果这是您的分隔符,您可以将分割更改为;

答案 1 :(得分:0)

http://www.regular-expressions.info/email.html

有关于正则表达式和电子邮件的大量信息。我会用一个简单的分割。

http://www.somacon.com/p355.php

删除空格