var validate = (function(){ var messages = { isEmail : '输入正确格式邮箱', isPhoneNum : '输入正确手机号' }; var validateTypes ={ isEmail:function(value){ var reg = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/; if(!reg.test(value)){ return true; } return false; }, isPhoneNum:function(value){ var reg = /^1[34578]d{9}$/; if(!reg.test(value)){ return true; } return false; } }; return function(value,type){ if(validateTypes[type](value)){ return messages[type]; } } })(); console.log(validate("","isEmail")); //输入正确格式邮箱 console.log(validate("bilibiligo@gmail.com","isEmail")); //undefine console.log(validate("test","isPhoneNum")); //手机号 console.log(validate("18824156554","isPhoneNum")); //undefine