手机校验和邮箱校验

来源:互联网 发布:算法工程师年薪 编辑:程序博客网 时间:2024/04/28 00:06
package com.zhongan.cashew.controller.suggestion;


import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zhongan.shrimp.core.io.IResult;
import com.zhongan.shrimp.core.io.ResultBuilder;
import com.zhongan.shrimp.mgm.constant.MGMMessage;
import com.zhongan.shrimp.mgm.dto.MGMSuggestionDTO;
import com.zhongan.shrimp.mgm.service.MGMSuggestionService;


/**
 * 意见反馈
 * @author daichangbo
 * 2017年9月11日 下午4:13:28
 */
@Controller
@RequestMapping("feedBack")
public class FeedBackController {

private static final Logger logger = LoggerFactory.getLogger(FeedBackController.class);

@Autowired
private MGMSuggestionService mGMSuggestionService;


@RequestMapping("saveFeedBackInfo")
@ResponseBody
public IResult<String> saveFeedBackInfo(MGMSuggestionDTO mgmSuggestionDTO){

if(mgmSuggestionDTO == null){
return ResultBuilder.build(false, MGMMessage.PARAM_ERROR, "对象为空保存失败");
}
String mobile = mgmSuggestionDTO.getPhone();
if(StringUtils.isEmpty(mgmSuggestionDTO.getPhone())){
return ResultBuilder.build(false, MGMMessage.TEL_NULL_ERROR, "参数为空保存失败");
}else if(!mobile.matches("^1[3|4|5|7|8][0-9]\\d{4,8}$")){  
/*^匹配开始地方$匹配结束地方,[3|4|5|7|8]选择其中一个{4,8},\d从[0-9]选择 
            {4,8}匹配次数4~8    ,java中/表示转义,所以在正则表达式中//匹配/,/匹配""*/  
        //验证手机号码格式是否正确  
        return ResultBuilder.build(false, MGMMessage.OTP_LOGIN_PHONE_ERROR, "手机号码不正确保存失败");   
        }
String  email = mgmSuggestionDTO.getEmail();
if(StringUtils.isEmpty(email)){
return ResultBuilder.build(false, MGMMessage.EMAIL_NULL_ERROR, "邮箱为空保存失败");
}
if(checkEmail(email) == false){
return ResultBuilder.build(false, MGMMessage.EMAIL_ERROR, "邮箱格式不正确保存失败");
}

IResult<String> results = mGMSuggestionService.insertMgmSuggestion(mgmSuggestionDTO);
if(results.isSuccess()){
return ResultBuilder.build(true, MGMMessage.SUCCESS, "保存成功");
}
return ResultBuilder.build(false, MGMMessage.SYSTEM_ERROR, "保存失败");

}
/**
* 邮箱校验
* @param email
* @return
*/
public static  boolean checkEmail(String email){
 boolean flag = false;
 try{
  String check = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
  Pattern regex = Pattern.compile(check);
  Matcher matcher = regex.matcher(email);
  flag = matcher.matches();
 }catch(Exception e){
  logger.error("验证邮箱地址错误", e);
  flag = false;
 }
 return flag;
}

}
原创粉丝点击