Let’s take a look at what email addresses are available:
If we judge each email address one by one, it is obviously impossible.
A complete Internet email address consists of the following two parts, in the following format: Login name@hostname.Domain name
is separated by a symbol "@" that means "at" (at). The left side of the symbol is the other party's login name, and the right side is the complete host name, which consists of the host name and domain name. Among them, the domain name consists of several parts, each part is called a subdomain (Subdomain), and each subdomain is separated by a dot ".". Each subdomain will tell the user some information about this mail server.
Regular expression for key validation: var myreg = /^([.a-zA-Z0-9_-]) @([a-zA-Z0-9_-]) (.[a-zA -Z0-9_-]) /;
Validation input box:
//验证邮箱 function vailEmail(){ var email = jQuery("#email").val(); var flag = false; var message = ""; var myreg = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; if(email ==''){ message = "邮箱不能为空!"; }else if(!myreg.test(email)){ message = "请输入有效的邮箱地址!"; }else if(checkEmailIsExist()){ message = "该邮箱地址已经被注册!"; }else{ flag = true; } if(!flag){ //错误提示 //jQuery("#emailDiv").removeClass().addClass("ui-form-item has-error"); // jQuery("#emailP").html(""); //jQuery("#emailP").html("<i class=\"icon-error ui-margin-right10\"> <\/i>"+message); //jQuery("#email").focus(); }else{ //正确提示 //jQuery("#emailDiv").removeClass().addClass("ui-form-item has-success"); //jQuery("#emailP").html(""); //jQuery("#emailP").html("<i class=\"icon-success ui-margin-right10\"> <\/i>该邮箱可用"); } return flag; }
Write a method to verify it
//验证邮箱是否存在 function checkEmailIsExist(){ var email = jQuery("#email").val(); var flag = false; jQuery.ajax( { url: "checkEmail?t=" + (new Date()).getTime(), data:{email:email}, dataType:"json", type:"GET", async:false, success:function(data) { var status = data.status; if(status == "1"){ flag = true; } } }); return flag; }
Background handler:
@RequestMapping(value = "/checkEmail", method = RequestMethod.GET) public void checkEmail(HttpServletRequest request,HttpServletResponse response) { Map<String, Object> map = new HashMap<String, Object>(); try { String email = request.getParameter("email"); String status = "0"; //写查询语句,查询表里面是否存在该邮箱 //UserBaseInfo userBaseInfo = userService.findUserByEmail(email); //if(userBaseInfo!=null)status="1"; map.put("status", status); String data = JSONObject.fromObject(map).toString(); response.getWriter().print(data); response.getWriter().flush(); response.getWriter().close(); } catch (Exception ex) { } }
The above is the jquery example code to verify whether the email format is correct, using regular expressions: var myreg = /^([.a-zA-Z0-9_-]) @([a-zA -Z0-9_-]) (.[a-zA-Z0-9_-]) /;, you can give it a try.