您好,欢迎访问一九零五行业门户网

js内置对象正则表达式应用实例

本文主要和大家分享js内置对象正则表达式应用实例,主要以代码的方式和大家讲解,希望能帮助到大家。
//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); } });
以上就是js内置对象正则表达式应用实例的详细内容。
其它类似信息

推荐信息