js、正则表达式验证有效手机号与身份证号

6.1 email的判断。
function ismail(mail)
{
return(new RegExp(/^\w+((-\w+)|(\.\w+))*\-AT-[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(mail));
}
6.2 手机号码的验证
6.3 身份证的验证
function isIdCardNo(num)
{
if (isNaN(num)) {alert("输入的不是数字!"); return false;}
var len = num.length, re;
if (len == 15)
re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
else if (len == 18)
re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/);
else {alert("输入的数字位数不对!"); return false;}
var a = num.match(re);
if (a != null)
{
if (len==15)
{
var D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];
}
else
{
var D = new Date(a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];
}
if (!B) {alert("输入的身份证号 "+ a[0] +" 里出生日期不对!"); return false;}
}
return true;
}
-------------------------------------------------------------------------------------
3.7 复选框的全选,多选,全不选,反选
全选
全选
function checkAll(str)
{
var a = document.getElementsByName(str);
var n = a.length;
for (var i=0; ia.checked = window.event.srcElement.checked;
}
function checkItem(str)
{
var e = window.event.srcElement;
var all = ("document.hrong."+ str);
if (e.checked)
{
var a = document.getElementsByName(e.name);
all.checked = true;
for (var i=0; i{
if (!a.checked){ all.checked = false; break;}
}
}
else all.checked = false;
}
3.8 文件上传过程中判断文件类型
-------------------------------------------------------------------------------------
.身份证严格验证:
.验证IP地址
function isip(s){
var check=function(v){try{return (v=0)}catch(x){return false}};
var re=s.split(".")
return (re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false
}
var s="202.197.78.129";
alert(isip(s))
.加sp1后还能用的无边框窗口!!


var CW_width = 400;
var CW_height = 300;
var CW_top = 100;
var CW_left = 100;
var CW_url = "/";
var New_CW = window.createPopup();
var CW_Body = New_CW.document.body;
var content = "";
var CSStext = "margin:1px;color:black; border:2px outset;border-style:e­xpression(onmouseout=onmouseup=function(){this.style.borderStyle='outset'}, onmousedown=function(){if(event.button!=2)this.style.borderStyle='inset'});background-color:buttonface;width:16px;height:14px;font-size:12px;line-height:11px;cursor:Default;";
//Build Window
include.startDownload(CW_url, function(source){content=source});
function insert_content(){
var temp = "";
CW_Body.style.overflow = "hidden";
CW_Body.style.backgroundColor = "white";
CW_Body.style.border = "solid black 1px";
content = content.replace(/]*)>/g,"");
temp += "";
temp += "";
temp += "Chromeless Window For IE6 SP1";
temp += "";
temp += "?";
temp += "0";
temp += "1";
temp += "x";
temp += "";
temp += "";
temp += content;
temp += "
"
;
temp += "";
CW_Body.innerHTML = temp;
}
setTimeout("insert_content()",1000);
var if_max = true;
function show_CW(){
window.moveTo(10000, 10000);
if(if_max){
New_CW.show(CW_top, CW_left, CW_width, CW_height);
if(typeof(New_CW.document.all.include)!="undefined"){
New_CW.document.all.include.style.width = CW_width;
New_CW.document.all.Max.innerText = "1";
}
}else{
New_CW.show(0, 0, screen.width, screen.height);
New_CW.document.all.include.style.width = screen.width;
}
}
window.onfocus = show_CW;
window.onresize = show_CW;
// Move Window
var drag_x,drag_y,draging=false
function drag_move(e){
if (draging){
New_CW.show(e.screenX-drag_x, e.screenY-drag_y, CW_width, CW_height);
return false;
}
}
function drag_down(e){
if(e.button==2)return;
if(New_CW.document.body.offsetWidth==screen.width && New_CW.document.body.offsetHeight==screen.height)return;
drag_x=e.clientX;
drag_y=e.clientY;
draging=true;
e.srcElement.setCapture();
}
function drag_up(e){
draging=false;
e.srcElement.releaseCapture();
if(New_CW.document.body.offsetWidth==screen.width && New_CW.document.body.offsetHeight==screen.height) return;
CW_top = e.screenX-drag_x;
CW_left = e.screenY-drag_y;
}
电话号码的验证
要求:
  (1)电话号码由数字、"("")""-"构成
  (2)电话号码为3到8位
  (3)如果电话号码中包含有区号,那么区号为三位或四位
  (4)区号用"("")""-"和其他部分隔开
  (5)移动电话号码为11或12位,如果为12位,那么第一位为0
  (6)11位移动电话号码的第一位和第二位为"13"
  (7)12位移动电话号码的第二位和第三位为"13"
  根据这几条规则,可以与出以下正则表达式:
  (^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)
function PhoneCheck(s) {
var str=s;
var reg=/(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/
alert(reg.test(str));
}
--------------------------------------------------------------------------------------
---------------------------------------------------------------------------------
//检验法人代码
function isCorporationCode(s){
var patrn=/^(\d){15}$/;
if (!patrn.exec(s)) return false
return true
}
//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}
//校验用户姓名:只能输入1-30个以字母开头的字串
function isTrueName(s)
{
var patrn=/^[^`~!@#$%^&*()+-=|\\\[\]\{\}:;\'\,./? 0-9]{2,19}$/;
if (!patrn.exec(s)) return false
return true
}
//校验密码:只能输入6-15个字母、数字
function isPasswd(s)
{
var patrn=/^[a-zA-Z0-9]{6,15}$/;
if (!patrn.exec(s)) return false
return true
}
//检验体重
function isAvoirdupois(s)
{
var patrn=/^[1-9]{1}[0-9]{0,2}$/;
var patrn2=/^[1-9]{1}$/;
var intPart=s.indexOf('.');
var decPart=s.lastIndexOf('.');
if (intPart==-1 && patrn.exec(s) && (s)>=2 && (s)200) return false
return true
}
//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
function isTel(s)
{
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
//校验手机号码:必须以数字开头,除数字外,可含有“-”
function isMobile(s)
{
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
//校验地区代码
function isAreaCode(s)
{
var patrn=/^(\d){6}$/;
if (!patrn.exec(s)) return false
return true
}
//校验邮政编码
function isPostalCode(s)
{
var patrn=/^[1-9]{1}(\d){5}$/;
if (!patrn.exec(s)) return false
return true
}
//校验搜索关键字
function isSearch(s)
{
var patrn=/^[^`~!@#$%^&*()+=|\\\[\]\{\}:;\'\,./? ]{1}[^`~!@$%^&()+=|\\\[\]\{\}:;\'\,.?]{0,19}$/;
if (!patrn.exec(s)) return false
return true
}
//校验是否为ip地址
function isIP(s)
{
var patrn=/^[0-9.]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}
//检验页码是否正确
function isPage(s)
{
var patrn=/^[1-9]{1}[0-9]{0,2}$/;
if (!patrn.exec(s)) return false
return true
}
//表单输入值错误提示
function vErr(o,s){
alert(s);
if (o) o.focus();
return false;
}
//判断单选
function chkRadio(o){
for (i=0;iif (o.checked) return true;
}
return false;
}

 

posted @ 2012-03-08 09:04  xyzhuzhou  阅读(5539)  评论(0编辑  收藏  举报