首页 | IT新闻 | 硬件 | 操作系统 | 开发 | 网络编程 | 数据库 | 热门框架 | 网络安全 | 组网 | 建站指南 | 网页制作 | 特效 | 实用技巧 | 服务器 | 办公 | QQ | 探索 | 社区

  • 技术部落
  • 部落首页 > 网络编程 > JavaScript > 正文
  • 收集整理些有用的JAVASCRIPT小知识
      2007-3-1  来源:网络资源  编辑:Jsbulo  热度:

    前段时间一直在做一个人才网站,里面用到了大量的Javascript代码,虽说很多都是从百度和别的网站上拷下来的,不过要把它们正确的用到自己网站上也是花费了我不少功夫的。其中有几段代码,是我找了n久也找不到完整的合适的,于是自己编出来的。现在就把它们贴出来,希望能帮到像我一样菜的鸟!
     
    一、身份证验证
    很多网页上都有身份证验证,不过大多是vbscript,javascript的n少。而且还不全面。身份证号码的组成结构,就不用我赘述了吧,在百度上一搜一大把的。
    就说怎么验证得了:身份证验证首先是要验证字数、以及除了末尾能用字母外其它的都应该是数字。其次就是两点最重要的,出生年月日和户口所在地。

    以下是引用片段:
    function isValidDate( year, month, day )
    {
       year  = parseInt(year,10);
       month = parseInt(month,10);
       day   = parseInt(day,10);
       if (( month==4) || (month==6) || (month==9) || (month==11) )
       { if (( day < 1) || (day > 30) )
         { alert( "日期在1 - 30之间" );
           return (false);
         }
       }
       else
       { if ( month!=2 )
         { if ( (day < 1) || (day > 31 ))
           {  alert( "日期在1 - 31之间" );
              return (false);
           }
         }
         else
         { // month == 2
           if ( ( year % 100 ) != 0 && (year % 4 == 0) || ( year % 100 ) == 0 && ( year % 400) == 0 )
           { if ( day > 29 )
             {  alert( "日期在1 - 29之间" );
                return (false);
             }
           }
           else
           { if ( day > 28 )
             { alert( "日期在1 - 28之间" );
               return (false);
             }
           }
         }
       }
     return (true);
    }
    function isIdCardNo(s)
    {
     if ((s.length <15)||(s.length ==16)||(s.length ==17)||(s.length >18)){
       window.alert("身份证位数不正确!");
       return false;
      }
     
        slen=s.length-1;//身份证除最后一位外,必须为数字!
     for (i=0; i<slen; i++)
     {
      cc = s.charAt(i);
      if (cc <"0" || cc >"9")
      {
       return false;
      }
     }
     //提取年月日
     var strYear = parseInt(s.substring(6,10))
     var strMonth = parseInt(s.substring(10,12))
     var strDay =  parseInt(s.substring(12,14))
    // Form1.year的代码根据个人情况变动
    var vYear=parseInt(document.Form1.year.options[document.Form1.year.selectedIndex].text)
     var vMonth=parseInt(document.Form1.month.options[document.Form1.month.selectedIndex].text)
     var vDay=parseInt(document.Form1.day.options[document.Form1.day.selectedIndex].text)
     //判断输入的出生年月日是否是正确的日期
     if(!isValidDate(strYear,strMonth,strDay))
     {
      window.alert("请检查身份证是否输入正确!");
       return false;
     }
    //判断是否与出生年月日吻合
     if((strYear!=vYear)||(strMonth!=vMonth)||(strDay!=vDay))
        {
      window.alert("身份证号与输入的出生年月日不符!");
       return false;
     }
     //document.Form1.ceshi.value=strMonth
     //document.Form1.ceshi2.value=vMonth
     return true;
    }
    因为我不知道身份证上户口所在地的值是用什么规则规定的,而且也没必要验证的那么详细。就没有做户口验证。

    不过我觉得,应该是大同小异的吧,只要判断form里户口选择select的值和身份证里户口对应的位数是否相等就ok了。
    s.substring(6,10)就是用来截取字段的。从第六位到第十位。
     
    二、排序的小代码
       做到排序时,我第一个想到的笨方法是手动输入,后来考虑到分类可能会很多,于是就想到了用过的那种点击向上、向下按钮就自动向上和向下的东东,例如qq空间里头播放器排序。
       于是上网搜了一下,结果很遗憾,调数据库的没有。没办法只得自己研究了一个。用起来也还行,自我感觉差不了多少。
    代码如下:

    以下是引用片段:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
    <!--#include file="../conn.asp"-->
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>Untitled Document</title>
    <STYLE type=text/css>TD {
     FONT-SIZE: 12px
    }
    SELECT {
     FONT-SIZE: 12px
    }
    INPUT {
     FONT-SIZE: 12px
    }
    A {
     TEXT-DECORATION: none
    }
    A:hover {
     COLOR: #c90000; TEXT-DECORATION: none
    }
    </STYLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    //window.onload = initPage;
    //function initPage()
    //{
     //   btnPutUp.onclick    = putUp;
     //   btnPutDown.onclick    = putDown;
     //   btnGoUp.onclick    = goUp;
      //  btnGoDown.onclick    = goDown;
    //}
    function putUp()
    {
        var strTempValue;
        var strTempText;
        var intCurIndex;
       
        intCurIndex = document.paixu.sltFruit.selectedIndex;
        //alert("intCurIndex: " + intCurIndex);
        if (intCurIndex > 0)
        {
            strTempValue= document.paixu.sltFruit.options.item(intCurIndex).value;
            strTempText    = document.paixu.sltFruit.options.item(intCurIndex).text;
      strTemp=document.paixu.sltFruit.options.item(intCurIndex - 1).value
          //  alert(strTempText + " - " + strTempValue);
           
            document.paixu.sltFruit.options.item(intCurIndex).value    = document.paixu.sltFruit.options.item(intCurIndex - 1).value;
            document.paixu.sltFruit.options.item(intCurIndex).text= document.paixu.sltFruit.options.item(intCurIndex - 1).text;
            document.paixu.sltFruit.options.item(intCurIndex - 1).value= strTempValue;
            document.paixu.sltFruit.options.item(intCurIndex - 1).text= strTempText;
           document.paixu.sltFruit.selectedIndex = intCurIndex - 1;
        intCur=intCurIndex - 1
        //shang=eval("document.paixu."+"slt"+intCurIndex+".value")-1;
        eval("document.paixu."+"slt"+intCurIndex+".value=strTemp")
        //xia=eval("document.paixu."+"slt"+intCur+".value")-(-1);
        eval("document.paixu."+"slt"+intCur+".value=strTempValue")
        //alert( ss);
        //document.paixu.slt"+intCurIndex+".value=eval("document.paixu."+"slt"+intCurIndex+".value")-1;
        //document.paixu."slt"+intCurIndex+".value"=document.paixu.("slt"+intCurIndex).value-1;
        }
    }
    function putDown()
    {
        var strTempValue;
        var strTempText;
        var intCurIndex;
        var intIndexCount;
       
        intCurIndex    = document.paixu.sltFruit.selectedIndex;
        intIndexCount    = document.paixu.sltFruit.length;
     
        //alert("intCurIndex: " + intCurIndex);
        //alert("intIndexCount: " + intIndexCount);
       
        if (intCurIndex < intIndexCount - 1)
        {
            strTempValue= document.paixu.sltFruit.options.item(intCurIndex).value;
            strTempText    = document.paixu.sltFruit.options.item(intCurIndex).text;
      strTemp=document.paixu.sltFruit.options.item(intCurIndex +1).value
            //alert(strTempText + " - " + strTempValue);
           
            document.paixu.sltFruit.options.item(intCurIndex).value    = document.paixu.sltFruit.options.item(intCurIndex + 1).value;
            document.paixu.sltFruit.options.item(intCurIndex).text        = document.paixu.sltFruit.options.item(intCurIndex + 1).text;
           document.paixu.sltFruit.options.item(intCurIndex + 1).value= strTempValue;
            document.paixu.sltFruit.options.item(intCurIndex + 1).text    = strTempText;
            document.paixu.sltFruit.selectedIndex = intCurIndex + 1;
       intCur=intCurIndex + 1
        //shang=eval("document.paixu."+"slt"+intCurIndex+".value")-1;
        eval("document.paixu."+"slt"+intCurIndex+".value=strTemp")
        //xia=eval("document.paixu."+"slt"+intCur+".value")-(-1);
        eval("document.paixu."+"slt"+intCur+".value=strTempValue")
        }
    }
    function goUp()
    {
        var intCurIndex;
        intCurIndex = sltCountry.selectedIndex;
        if (intCurIndex > 0)
            sltCountry.selectedIndex = intCurIndex - 1;
    }
    function goDown()
    {
        var intCurIndex;
        var intIndexCount;
       
        intCurIndex        = sltCountry.selectedIndex;
        intIndexCount    = sltCountry.length;
       
        if (intCurIndex < intIndexCount - 1)
            sltCountry.selectedIndex = intCurIndex + 1;
    }
    //-->
    </SCRIPT>
    <body>
    <%’存储数据库的过程
    n=trim(request("num"))
    if n<>"" then
     i=1
     y=0
     do while not n<0
     conn.execute("update BigClass_New set paixu="&i&" where BigClassID="&trim(request(("slt")&y))&"")
    ’我用的办法是在数据库表里加个字段定义排序,然后显示时就靠它了
    ’trim(request(("slt")&y))是动态获取input的值,做循环的的时候
    ’都这么用
     n=n-1
     i=i+1
     y=y+1
     loop
     response.Write("<SCRIPT>alert(’修改成功’);location=’News_ClassManage.asp’;</SCRIPT>")
     ’response.End()
    else
    %>
    <div align="center">
    <table class="border" border="0" cellspacing="2" width="620" cellpadding="0" style="word-break:break-all">
              <%
    Set rs= Server.CreateObject("ADODB.Recordset")
    sql="select * from BigClass_New order by paixu"
    rs.open sql,conn,1,1
    if not rs.eof then
    ’我的数据库表名叫BigClass_New%>
    <form name="paixu" method="post" action="News_ClassPaixu.asp">
        <tr bgcolor="#A4B6D7" class="title">
                <td width="50%" height="55" align="right">
       <div align="right">
       <SELECT NAME="sltFruit" size="<%=rs.recordcount%>">
       <%do while not rs.eof %>
       <OPTION VALUE="<%=trim(rs("BigClassID"))%>"><%=trim(rs("BigClassName"))%></OPTION>
       <%rs.movenext
       loop%>
       </SELECT>
       
       </div>
       </td>
                <td width="40"  height="25" align="center">
       <INPUT TYPE="BUTTON" ID="btnPutUp" VALUE="上移"  onClick="javascript:return putUp();">
       <INPUT TYPE="BUTTON" ID="btnPutDown" VALUE="下移" onClick="javascript:return putDown();">
       </td>
              </tr>
        <%rs.movefirst
       i=0
       do while not rs.eof %>
       <input type="hidden" name="slt<%=i%>" value="<%=trim(rs("BigClassID"))%>">
       <%
       rs.movenext
       i=i+1
       loop%>
       <input type="hidden" name="num" value="<%=i-1%>">
         <tr bgcolor="#A4B6D7" class="title">
                <td width="560" height="55" align="center" colspan="2">
        <input type="submit" name="save" value="保存">
       </td>
              </tr>
        </form>
       <%else%>
       <tr bgcolor="#A4B6D7" class="title">
                <td width="36" height="55" align="center" colspan="2">
       <%response.Write("请先输入大类!")%>
       </td>
              </tr>
       </table>
       </div>
    <%end if%>

    <%end if%>
    </body>
    </html>
    原理是按照BigClass_New表循环定义input,input的值就是目前的排序,这样就与select里的值对应起来了。再运行函数,使上下移动时,增减input里的值,也就是按照select里的排序顺序,修改了input里的值,这样再存储到库里的就是正确的排序了。

     
    不晓得我这样的表达算不算清楚。哈哈!
    暂时就举这么两个例子吧。
    最后补充一句:不会您就百度吧。