一个通用验证javascript函数

发表于:2007-06-30来源:作者:点击数: 标签:
很久以前写的一个通用验证函数,大家不要笑啊,许多东西没有完善比如日期验证。 SCRIPT LANGUAGE= Java Script 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
很久以前写的一个通用验证函数,大家不要笑啊,许多东西没有完善比如日期验证。
<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)
                            {
                                sign=-4;
                                break;
                            }
                            else{
                            document.form1.all(i).value=parseFloat(str);
                            }
                        }
                if (l.indexOf(@#int@#)==0)//在名字前有int表示整数
                        {
                            var re=/^\d{0,8}$/;
                            var str=document.form1.all(i).value;
                            var r=str.match(re);
                            if (r==null)
                            {
                            sign=-5;
                            break;
                            }
                            else{
                            document.form1.all(i).value=parseInt(str);
                            }
                        }
                if (l.indexOf(@#email@#)==0)//在名字前有email表示email
                        {
                            var re=/^\w+@\w+\.\w{2,3}/;
                            var str=document.form1.all(i).value;
                            var r=str.match(re);
                            if (r==null)
                            {
                            sign=-6;
                            break;
                            }
                        }
                
            }
            break;
    case @#SELECT@#:    
                    if (document.form1.all(i).options[document.form1.all(i).selectedIndex].value=="")
                    {
                        sign=-7;
                    }
            break;
    case @#TEXTAREA@#:
                    if (document.form1.all(i).value.length>32000)
                    {
                        sign=-8;
                    }
            break;
    default :
            break;    
    }
    }
    switch(sign)
    {
        case -1:
            alert(@#请填写所有的必填项@#);
            return;
            break;
        case -2:
            alert("请按以下格式填写日期: yyyy-mm-dd!");
            return;
            break;
        case -3:
            alert(@#请填写正确的日期@#);
            return;
            break;
        case -4:
            alert("请填写正确的金额 eg:12312424.23!");
            return;
            break;
        case -5:
            alert("请填写正确的数字 eg:12312!");
            return;
            break;
        case -6:
            alert("请填写合法的email eg:Yourname@domain.com!");
            return;
            break;
        case -7:
            alert("请在下拉列表中选择!");
            return;
            break;
        case -8:
            alert("内容不得超过64K!");
            return;
            break;
        case 1:
            document.form1.submit();
            break;
    }
}

原文转自:http://www.ltesting.net