Home > Web Front-end > JS Tutorial > Example of jquery verifying whether the email format is correct_jquery

Example of jquery verifying whether the email format is correct_jquery

WBOY
Release: 2016-05-16 15:32:14
Original
1578 people have browsed it

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;
     }
Copy after login

Write a method to verify it

 //验证邮箱是否存在
     function checkEmailIsExist(){
       var email = jQuery("#email").val();
       var flag = false;
       jQuery.ajax(
        { url: "checkEmail&#63;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;
     }
Copy after login

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) {
      
    }
  }
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template