Home >Web Front-end >JS Tutorial >js built-in object regular expression application example

js built-in object regular expression application example

小云云
小云云Original
2018-03-28 17:29:071827browse

This article mainly shares with you the application examples of regular expressions for built-in objects in js. It mainly explains it to you in the form of code. I hope it can help you.

//1、身份证正则表达式验证
    function checkIdNo(){ 
       var idno = $("#idno").val();
       //15位数身份证验证正则表达式:
       var isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/; 
       //18位数身份证验证正则表达式 :
       var isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    if(!(isIDCard1.test(idno)||isIDCard2.test(idno))){ 
          $("#idno").css("color","red");
          $("#idnocheck").css("color","red");
          $("#idnocheck").text('请输入正确的身份证号');
    }else{
          $("#idno").css("color","black");
          $("#idnocheck").text('格式正确');
          $("#idnocheck").css("color","green");     
          }
       }

    //2、电话正则表达式
    function checkPhone(){ 
      var phone = $("#cellphone").val();
    if(!(/^1[34578]\d{9}$/.test(phone))){ 
          $("#cellphone").css("color","red");
          $("#phone").css("color","red");
          $("#phone").text('请输入正确的电话号码, 格式:区号-号码 或 区号-号码-分机号');
    }else{
          $("#cellphone").css("color","black");
          $("#phone").text('格式正确');
          $("#phone").css("color","green");     
          }
     }               
    //3、邮箱正则
      function checkEmail(){ 
         var uEmail = $("#uEmail").val();
         var uEmailCheck =  /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;   
    if(!(uEmailCheck.test(uEmail))){ 
          $("#uEmail").css("color","red");
          $("#uEmailCheck").css("color","red");
          $("#uEmailCheck").text('请输入正确邮箱');
    }else{
          $("#uEmail").css("color","black");
          $("#uEmailCheck").text('格式正确');
          $("#uEmailCheck").css("color","green");     
          }
       }       

   // 邮箱自动补全
     $("#uEmail").autocomplete({
        delay:0,
        //autoFoucs:true,
        source:function(request,response){
            var hosts = ['qq.com','163.com','126.com','sina.com.cn','263.com'],
                term = request.term, //获取用户输入的内容
                name =term,          //邮箱的用户名
                host ='',            //邮箱的域名
                ix = term.indexOf('@'),  //@的位置
                result = [];
                
            //当有@的时候,重新分配用户名和域名
            if(ix > -1){
                name =term.slice(0,ix);
                host = term.slice(ix+1);
            }
            if(name){
                //如果用户已经输入@和后面的域名,
                //那么就找到相关的提示,比如bnbbs@1,就提示bnbbs@163.com
                //如果用户还没有输入@,那就提示所有域名
                var findedHosts = [];
                if(host){
                    findedHosts = $.grep(hosts,function(value,index){
                        return  value.indexOf(host) > -1
                    });
                }else{
                    findedHosts =hosts;
                }
                var findedResult = $.map(findedHosts,function(value,index){
                    return name+'@'+value;
                })
                if(findedResult==''){
                    result.push(term)
                }

                result = result.concat(findedResult);
            }
            response(result);

        }
    });

The above is the detailed content of js built-in object regular expression application example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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