当前位置:编程学习 > asp >>

一个通用验证javascript函数

答案:很久以前写的一个通用验证函数,大家不要笑啊,许多东西没有完善比如日期验证。
<SCRIPT LANGUAGE="javascript" src="valid.js">
</SCRIPT>

<TABLE cellspacing=0 cellpadding=0 border=0 align=center>
<FORM METHOD=POST ACTION="bbs_new.asp" name="form1" id="form1">
<TR>
    <TD>姓 名:<INPUT TYPE="text" NAME="txtName" class=smallinput re=1 size=22></TD>
    <TD>密 码:<INPUT TYPE="password" NAME="txtPWD" class=smallinput re=1 size=22></TD>
</TR>
<TR>
    <TD colspan=2>主 题:<INPUT TYPE="text" NAME="txtTitle" class=smallinput size=53 re=1></TD>
</TR>
<TR>
    <TD colspan=2>表 情:<input type=hidden name=City_StreetID value=<%=Request("City_StreetID")%> ></TD>
</TR>
<TR>
    <TD colspan=2><%ShowExpression%></TD>
</TR>
<TR>
    <TD>内 容:</TD>
    <TD>
    <INPUT TYPE="button" name="butn" class=buttonface value="提交" onclick="javascript:ck();" />
      
    <INPUT TYPE="reset" Value="清除" class=buttonface name="reset" />
    </TD>
</TR>
<TR>
    <td colspan=2>
    <textarea NAME="txtContent" cols="49" rows="15"></textarea>
    </td>
</TR>
</FORM>
</TABLE>
//valid.js
function ck()
{
    var sign=1;
    var s;
    var l;
    for(i=0; i<document.form1.all.length; i++) {
    s=document.form1.all(i).tagName;
    l=document.form1.all(i).name;
    switch(s)
    {case 'INPUT':
            if(document.form1.all(i).value=='')
            {
                if (document.form1.all(i).re==1)//re表示必须
                {
                    sign=-1;
                    break;
                }
                
            }
            else
            {    
                if (l.indexOf('dat')==0)//在名字前有dat表示日期
                {
                    var re=/^\d{4}-\d{1,2}-\d{1,2}$/;
                    var str=document.form1.all(i).value;
                    var r=str.match(re);
                    if (r==null)
                        {
                            sign=-2;
                            break;
                        }
                    else{
                        var s=str.split("-");
                        if (s[0].substring(0,2)<19 || s[1]>12 || s[1]<1 || s[2]>31 || s[2]<1)
                            {
                                sign=-3;
                                break;
                            }
                        }
                }
                if (l.indexOf('float')==0)//在名字前有float表示浮点数
                        {
                            var re=/^\d{1,8}$|\.\d{1,2}$/;
                            var str=document.form1.all(i).value;
                            var r=str.match(re);
                            if (r==null)
                            {
                               &

上一个:[问题解决]IE中可以将弹出窗口置于最顶层的方法(即使当前焦点不在,比如最小化了那个IE也能弹出来)
下一个:非常好用的日期选择器建议入精华《二》

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,