常用JS代码

  1. >
  2. /* ****************************************** />
  3. 获取文档元素
  4. ****************************************** */
  5. function $(id){ />
  6. return document.getElementById(id) ; />
  7. }
[@more@]

/* ******************************************
检查用户是否输入数字
ascii码说明:
8:退格键
46:delete
37-40: 方向键
48-57:小键盘区的数字
96-105:主键盘区的数字
110、190:小键盘区和主键盘区的小数点
189、109:小键盘区和主键盘区的负号
13:回车
9: Tab 就是那个把焦点移到下一个文本框的东东。
****************************************** */
function checkNum(){
var k=window.event.keyCode;
if (!((k==46)||(k==8)||(k>=48 && k<=57)||k==13))
{
alert("只能输入数值");
window.event.keyCode = 8 ;
}
}

/* ******************************************
回车转Tab键
window.body.οnkeypress=enterToTab()
****************************************** */
function enterToTab(){
var k=window.event.keyCode;
if(k==13){
window.event.keyCode = 9;
}
}

/* ******************************************
无提示静态关闭当前页面
****************************************** */
function exitPage(){
//免弹框设置
window.opener = null ;
//解决opener=null在IE7中失效问题
window.open("","_self");
window.close();
}


/* ******************************************
用户名非空+长度+合法性验证
验证规则:用户名由字母、数字和下划线构成,4-16位,并以字母或下划线开始
****************************************** */
function checkUserName(userName){
var pattern = /^[a-zA-Z_]w{3,15}$/gi;
if(!pattern.test(userName.value)){
$("err"+userName.id).style.display = "inline";
return false;
}
$("err"+userName.id).style.display = "none";
return true;
}

/* ******************************************
非空内容
****************************************** */
function checkNull(vnull){
$("err"+vnull.id).style.display = "none";
if(vnull.value==""){
$("err"+vnull.id).style.display = "inline";
return false;
}
return true;
}


/* ******************************************
密码非空+长度+密码确认验证
****************************************** */
function checkPass(pass,rpass){
if(rpass!=null){
if(checkRPass(rpass)){
if(pass.value!=rpass.value){
$("err"+rpass.id).style.display = "inline";
rpass.select();
return false;
}else{
$("err"+rpass.id).style.display = "none";
return true;
}
}else{
return false;
}
}else{
return checkRPass(pass);
}
}

function checkRPass(pass){
var pattern = /^w{6,16}$/gi;
$("err"+pass.id).style.display = "inline";
if(pass.value==""){
return false;
}else if(!pattern.test(pass.value)){
return false;
}else{
$("err"+pass.id).style.display = "none";
return true;
}
}






/* ******************************************
电子邮件验证
****************************************** */

function checkEmail(strEmail){
$("err"+strEmail.id).style.display = "none";
var pattern = /^w.+@w+..+$/gi;
if(!pattern.test(strEmail.value)){
$("err"+strEmail.id).style.display = "inline";
return false;
}
return true;
}

/* ******************************************
电话验证
固定电话、小灵通:type=1
形如:(0512)65162568 0512-65162568
021-65162568 021-6516256 65162568
手机号码:type=2
形如:013218002686 13218002686 (0)13218002686
其它号码:type=3
****************************************** */
function checkTelNo(objTel,type){
$("err"+objTel.id).style.display = "none";
var tel = /^((d{3,4})|d{3,4}-)?d{7,8}$/gi;
var phone = /^((0)|0)?1d{10}$/gi;

if(tel.value==""){
$("err"+objTel.id).style.display = "inline";
return false;
}

if(type==1){
if(!tel.test(objTel.value)){
$("err"+objTel.id).style.display = "inline";
return false;
}
return true;
}else if (type==2){
if(!phone.test(objTel.value)){
$("err"+objTel.id).style.display = "inline";
return false;
}
return true;
}else{
return true;
}

}

/* ******************************************
身份证验证
身份证号码为15位或18位
****************************************** */
function checkIDCard(idCard){
$("err"+idCard.id).style.display = "none";
var pattern = /^d{15}b|bd{18}$/gi;
if(!pattern.test(idCard.value)){
$("err"+idCard.id).style.display = "inline";
return false;
}
return true;
}


/* ******************************************
表单提交
****************************************** */
function checkForm(){
//var isOk = false ;
//isOk = checkUserName($("txtUserId")) ? isOk :false;
if(checkUserName($("txtUserId")) //登录名
&&checkPass($("txtPassword"),null) //密码
&&checkPass($("txtPassword"),$("txtPassword2")) //确认密码
&&checkNull($("txtUserName")) //真实姓名
&&checkNull($("txtPwdQue")) //密码问题
&&checkNull($("txtPwdAns")) //问题答案
&&checkEmail($("txtEmail")) //电子邮件
&&checkTelNo($("txtTelNo"),2) //联系电话
&&checkNull($("txtAddress")) //联系地址
&&checkIDCard($("txtIDCard")) //身份证号码
){
return true;
}
return false;
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ppjava2009/archive/2010/02/24/5323326.aspx#

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23493047/viewspace-1033129/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值