手机校验和邮箱校验
来源:互联网 发布:算法工程师年薪 编辑:程序博客网 时间: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;
}
}
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;
}
}
阅读全文
0 0
- 手机校验和邮箱校验
- js手机和邮箱正则校验
- 邮箱校验
- Java 正则校验(手机,邮箱,固话........)
- js手机号码校验,邮箱校验
- 邮箱校验以及电话号码校验
- js校验座机和手机
- Java正则表达式校验邮箱和手机号
- Java正则表达式校验邮箱和手机号
- Java正则表达式校验邮箱和手机号
- Java正则表达式校验邮箱和手机号
- Java正则表达式校验邮箱和手机号
- Java正则表达式校验邮箱和手机号
- 电话,手机,微信账号,邮箱正则表达式校验
- android 校验用户名密码手机邮箱身份证邮编等
- email邮箱格式校验
- C++ Regex 邮箱校验
- C++ Regex 邮箱校验
- Java中集合Set的用法
- Ambari——大数据平台的搭建利器(Ambari 是什么)
- xml格式报文的拼装,和解析成实体类
- oracle nvl使用小技巧1
- 汇总
- 手机校验和邮箱校验
- Security-OAuth2 密码模式之服务器实现(工具IDEA+Maven+springboot)
- [洛谷P1550] [USACO08OCT]打井Watering Hole [最小生成树]
- Idea中Terminal命令不能执行git命令解决办法
- 大数据时代:Kafka 如何做到 1 秒发布百万条消息
- Assets 使用Pdf 矢量图
- 麦子金服:重视平台的安全性与合规性
- PullToRefresh+TabLayout+侧滑菜单栏+无限轮播小圆点+Fragment多条目+收藏网页数据到SQLite数据库
- 如何重载流操作符