邮箱正则校验

◇◆丶佛笑我妖孽 提交于 2019-11-27 07:43:22
var should = require('should');

describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {

      true.should.be.eql(verifyMultipleEmail("aa@qq.com"));
      false.should.be.eql(verifyMultipleEmail("aa,@qq.com"));

      true.should.be.eql(verifyMultipleEmail("aa@qq.com, cc@163.com"));
      false.should.be.eql(verifyMultipleEmail("aa@qq.com, ,cc@163.com"));
      false.should.be.eql(verifyMultipleEmail("aa@qq.com, cc@163.com,"));
      false.should.be.eql(verifyMultipleEmail(","));
      false.should.be.eql(verifyMultipleEmail("@"));

      true.should.be.eql(verifyMultipleEmail("lkj_jl@k.com"));
    });
  });
});



function verifyMultipleEmail(email) {
    var _result = false;
    var _temp = '';

    if(email.indexOf('\,') === (-1)) {
        _result = verifyEmail(email);
        return _result;
    }   
    
    var _array = email.split("\,");

    _result = true;
    _array.forEach(function(value, index, full) {
        _temp = value.replace(' ', '');
        if(!verifyEmail(_temp)) {
            _result = false;
        } 
    })
    return _result;
}

function verifyEmail(email) {
    var reg = /^[a-z_0-9.-]{1,64}@([a-z0-9-]{1,200}.){1,5}[a-z]{1,6}$/;
    return email.match(reg) ? true : false;
}

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!