关闭

JavaScript常用技巧

标签: javascriptfunctioninputborderexceptionclass
415人阅读 评论(0) 收藏 举报
分类:

//檢查身份證
function checkAgentId(agentId){
      if (agentId.length==10){
        if (((agentId.charAt(0)=="A") && (agentId.charAt(1)=="A"))||((agentId.charAt(0)=="A") && (agentId.charAt(1)=="Z"))){
           return true;
}else{
            checknum1=((agentId.charAt(0)>="A") && (agentId.charAt(0)<="Z"));
            checknum2=((agentId.charAt(1)==1) || (agentId.charAt(1)==2));
            if (checknum2 && checknum1){
               id1 = agentId.charAt(0);
               if (id1 == 'A') {id0=1;}
               else if (id1 == 'B') { id0=10 ;}
               else if (id1 == 'C') { id0=19 ;}
               else if (id1 == 'D') { id0=28 ;}
               else if (id1 == 'E') { id0=37 ;}
               else if (id1 == 'F') { id0=46 ;}
               else if (id1 == 'G') { id0=55 ;}
               else if (id1 == 'H') { id0=64 ;}
               else if (id1 == 'I') { id0=39 ;}
               else if (id1 == 'J') { id0=73 ;}
               else if (id1 == 'K') { id0=82 ;}
               else if (id1 == 'L') { id0=2  ;}
               else if (id1 == 'M') { id0=11 ;}
               else if (id1 == 'N') { id0=20 ;}
               else if (id1 == 'O') { id0=48 ;}
               else if (id1 == 'P') { id0=29 ;}
               else if (id1 == 'Q') { id0=38 ;}
               else if (id1 == 'R') { id0=47 ;}
               else if (id1 == 'S') { id0=56 ;}
               else if (id1 == 'T') { id0=65 ;}
               else if (id1 == 'U') { id0=74 ;}
               else if (id1 == 'V') { id0=83 ;}
               else if (id1 == 'W') { id0=21 ;}
               else if (id1 == 'X') { id0=3  ;}
               else if (id1 == 'Y') { id0=12 ;}
               else if (id1 == 'Z') { id0=30 ;}
               id2 = id0 + agentId.charAt(1)*8 + agentId.charAt(2)*7 + agentId.charAt(3)*6 + agentId.charAt(4)*5 + agentId.charAt(5)*4 + agentId.charAt(6)*3 + agentId.charAt(7)*2 + agentId.charAt(8)*1 + agentId.charAt(9)*1;
               if (id2 % 10 == 0){
                  return true;
   }
            }
         }
   }
   return false;
}

//轉換從UTF-8 到Big5 或 GB2312
function Unicode2Str(str){
var re=/&#[/da-fA-F]{1,5};/ig;
var arr=str.match(re);
if(arr==null)return("");
var size=arr.length;
var arr2=new Array(size);
for(var i=0;i<arr.length;i++){
arr2[i]=String.fromCharCode(arr[i].replace(/[&#;]/g,""));
}
for(var i=0;i<arr.length;i++){
str=str.replace(arr[i],arr2[i]);
}
//return(arr.toString().replace(/,/g,""))
return str;
}

//獲得當前客戶端時間 (Format: YYYYMMDD).
function getCurrentDate(){
var dateObj= new Date();
var dateString=dateObj.getFullYear();
if(dateObj.getMonth()<10){
dateString=dateString+'0'+dateObj.getMonth();
}else{
dateString+=dateObj.getMonth();
}
if(dateObj.getDate()<10){
dateString+='0'+dateObj.getDate();
}else{
dateString+=dateObj.getDate();
}
return dateString;
}  

//居中顯示彈出窗口
function PopWindowOnCenter(url,title,iwidth,iheight){
var ileft,itop;
ileft = (window.screen.width-iwidth)/2;
itop = (window.screen.height-iheight)/2;

window.open(url,title,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+iwidth+',height='+iheight+',left='+ileft+',top='+itop);
}

//移動選中的成員從objFromId的select到objToId的select
function moveTo(objFromId,objToId){
var objFrom = document.getElementById(objFromId);
var objTo = document.getElementById(objToId);

var len = objFrom.length;
for(var i=len-1; i>=0; i--){
if(objFrom.options[i].selected){
var j = 0;
for(j = 0; j < objTo.length; j++){
if(objFrom.options[i].value == objTo.options[j].value){
break;
}
}
if(j == objTo.length){//the selected item is not in objTo.
objFrom.options[i].selected = false;
var option1 = objFrom.options[i];
objTo.options.add(new Option(option1.text, option1.value));
objFrom.options.remove(i);
}
objFrom = document.getElementById(objFromId);
len = objFrom.length;
}
}
}

//移動全部成員從objFromId的select到objToId的select
function moveAllTo(objFromId,objToId){
var objFrom = document.getElementById(objFromId);
var objTo = document.getElementById(objToId);

var len = objFrom.length;
for(var i=len-1; i>=0; i--){
var j = 0;
for(j = 0; j < objTo.length; j++){
if(objFrom.options[i].value == objTo.options[j].value){
break;
}
}
if(j == objTo.length){//the selected item is not in objTo.
var option1 = objFrom.options[i];
objTo.options.add(new Option(option1.text, option1.value));
objFrom.options.remove(i);
}
objFrom = document.getElementById(objFromId);
len = objFrom.length;
}
}


//屏蔽非數字鍵
function checkKey(){
if(event.keyCode<48 || (event.keyCode>57 && event.keyCode<96)
|| event.keyCode>105){
if(event.keyCode != 8){
event.returnValue=false;
}
}
}
//用與樹狀列表的顯示
function showMenu(id,ulId){
var obj = document.getElementById(id);

if(obj.className == ""){
obj.className = "selected";
document.getElementById(ulId).style.display = "block";
}
else{
obj.className = "";
document.getElementById(ulId).style.display = "none";
}
}

//檢查str中是否包含漢字
function CheckChinese( str){
     var Strs = str;
     var i;
     for ( i=0;i<str.length;i++) {
        if(str.charCodeAt(i)>=1000){
                return true;
        }
     }
     return false;
}


//校验是否全由数字组成

程序代码
function isDigit(s)
{
var patrn=/^[0-9]{1,20}$/;
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=/^[a-zA-Z]{1,30}$/;
if (!patrn.exec(s)) return false
return true
}

//校验密码:只能输入6-20个字母、数字、下划线

程序代码
function isPasswd(s)
{
var patrn=/^(w){6,20}$/;
if (!patrn.exec(s)) return false
return true
}

//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”

程序代码
function isTel(s)
{
//var patrn=/^[+]{0,1}(d){1,3}[ ]?([-]?(d){1,12})+$/;
var patrn=/^[+]{0,1}(d){1,3}[ ]?([-]?((d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}

//校验手机号码:必须以数字开头,除数字外,可含有“-”

程序代码
function isMobil(s)
{
var patrn=/^[+]{0,1}(d){1,3}[ ]?([-]?((d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}

//校验邮政编码

程序代码
function isPostalCode(s)
{
//var patrn=/^[a-zA-Z0-9]{3,12}$/;
var patrn=/^[a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(s)) return false
return true
}

//校验搜索关键字

程序代码
function isSearch(s)
{
var patrn=/^[^`~!@#$%^&*()+=|][]{}:;',.<>/?]{1}[^`~!@$%^&()+=|][]{}:;',.<>?]{0,19}$/;
if (!patrn.exec(s)) return false
return true
}

程序代码
function isIP(s) //by zergling
{
var patrn=/^[0-9.]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}

/**
 * 功能:使指定值的指定名称的单复选框处于选中状态。
 * radioName:单选框组件名
 * val:指定值
 */
function makeRadioChecked(radioName, val) {
    var obj = document.all[radioName];
    try {
        if (obj) {
            if (obj.type == "radio" && obj.value == val) {
                obj.checked = true;
            }
            for (var i = 0; i < obj.length; i++) {
                if (obj[i].type == "radio" && obj[i].value == val) {
                    obj[i].checked = true;
                    break;
                }
            }
        }
    } catch(exception) {
        alert("error");
    }
}

自创javascrit分页代码。

function showpages(total,perpage,current,filename,seed,bShow){
//total总记录数,perpage每页记录数,current当前记录,filename文件名?page=,seed中间数字两边间隔数,bshow显示中间数字页面
//


var sRet,i,startPage,endPage,totalPage
//startPage:循环开始/endPage:循环结束/totalPage:总页数
//处理URL中的空格
if (filename!=''){
filename="&"+filename;
}
  if (total % perpage==0 ){

 totalPage=total/perpage;

}else{

totalPage=Math.floor(total/perpage)+1;
}


  if (totalPage<=10){
startPage=1;
}else{
if ((current-seed) >0) {
startPage=current-seed;
}else{
startPage=1;
}
}

if (totalPage<=10) {
endPage=totalPage
}else{
if ((current+seed)<totalPage) {
endPage=current+seed
}else{
endPage=totalPage
}
}

if (current<seed) {
if (totalPage>10){
endPage=10
}
}
 var sRet1=""

 if (bShow) {
for (i=startPage;i<=endPage;i++)
{

if (i==current)
{
sRet1=sRet1+"<font style='background-color:#CCCC33;'><b>"+current+"</b></font> "
}
else
{
sRet1=sRet1+"<a href=?page="+i+filename+">"+i+"</a> "
}

}
 }

    sRet=""
    //sRet=sRet+"<form name=jumpPage mothod=post action= >"
sRet=sRet+"<font class=tcat2>共"+total+"条,"
    sRet=sRet+"第"+current+"页/共"+ totalPage+"页, "
sRet=sRet+"<a href=?page=1"+ filename+">第一页</a> "
if ((current==1)&&(current!=totalPage)){


sRet=sRet+" 上一页 "+sRet1+" <a href=?page="+(current+1) +filename+">下一页</a>"
}else{
if (current>1) {

if  (current<totalPage) {
sRet=sRet+" <a href=?page="+(current-1)+filename+">上一页</a> "+sRet1+" <a href=?page="+(current+1)+filename+">下一页</a>"
}else{
if (current==totalPage) {
sRet=sRet+" <a href=?page="+(current-1)+filename+">上一页</a> "+sRet1+" 下一页"

}
    }
}else{
sRet=sRet+" 上一页 "+sRet1+" 下一页"
}
}
sRet=sRet+"  <a href=?page="+ totalPage+filename+">最末页</a>"
//sRet=sRet+"<input type=hidden name=wheretogo value=go>&nbsp;"
//sRet=sRet+"<input type=hidden name=maxpage value="+totalPage+">"
sRet=sRet+"  跳转到<input name=currentPage class=border1px size=3  onkeydown=if((event.keyCode==13)&&(this.value!='')&&(this.value!=0)&&(this.value<"+(totalPage+1)+"))window.location='?page='+this.value+'"+filename+"'; onkeyup=if(isNaN(this.value))this.value=''; >页 "
//sRet=sRet+"<input type=button value=GO class=border1px onclick=jump('"+filename+"');>&nbsp;"

sRet=sRet+"</font>"
//sRet=sRet+"</form>"
//alert(sRet);
document.write(sRet)

}

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:46522次
    • 积分:660
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:18篇
    • 译文:0篇
    • 评论:12条
    文章分类
    最新评论