Javascript实现的自动验证函数(原创)

test.htm

 1 None.gif <!--  #include file = " message.inc "   -->
 2 None.gif < HTML >
 3 None.gif < HEAD >
 4 None.gif < TITLE >  New Document  </ TITLE >
 5 None.gif     < meta http - equiv = " content-type "  content = " text/html; charset=shift_jis "   />
 6 None.gif     < meta http - equiv = " pragma "  content = " no-cache " >
 7 None.gif     < meta http - equiv = " cache-control "  content = " no-cache " >
 8 None.gif     < meta http - equiv = " expires "  content = " 0 " >
 9 None.gif < script src = " common.js " ></ script >
10 None.gif < script src = " validate.js " ></ script >
11 None.gif
12 None.gif </ HEAD >
13 None.gif
14 None.gif < BODY >
15 None.gif < FORM METHOD = POST ACTION = "" >
16 None.gifID < INPUT TYPE = " text "  id = " txtInt "  NAME = " txtInt "  maxlength = " 6 "  feildtype = " integer "  msg = " ,<%=IMSG0002%> "   >< br >
17 None.gifID < INPUT TYPE = " text "  id = " txtInt "  NAME = " txtInt "  maxlength = " 6 "  feildtype = " integer "  msg = " ,<%=IMSG0002%> "   >< br >
18 None.gifID < INPUT TYPE = " text "  id = " txtInt "  NAME = " txtInt "  maxlength = " 6 "  feildtype = " integer "  msg = " ,<%=IMSG0002%> "   >< br >
19 None.gifID < INPUT TYPE = " text "  id = " txtInt "  NAME = " txtInt "  maxlength = " 6 "  feildtype = " integer "  msg = " ,<%=IMSG0002%> "   >< br >
20 None.gif
21 None.gif名称 < INPUT TYPE = " text "  id = " txtString "  NAME = " txtString "  maxlength = " 5 "  feildtype = " string "  msg = " <%=IMSG0003%>,<%=IMSG0004%> "  mustitem = " true " >< br >
22 None.gif名称 < INPUT TYPE = " text "  id = " txtString "  NAME = " txtString "  maxlength = " 5 "  feildtype = " string "  msg = " <%=IMSG0003%>,<%=IMSG0004%> "  mustitem = " true " >< br >
23 None.gif名称 < INPUT TYPE = " text "  id = " txtString "  NAME = " txtString "  maxlength = " 5 "  feildtype = " string "  msg = " <%=IMSG0003%>,<%=IMSG0004%> "  mustitem = " true " >< br >
24 None.gif
25 None.gif名称2 < INPUT TYPE = " text "  id = " txtStr "  NAME = " txtStr "  maxlength = " 5 "  feildtype = " string "  msg = " <%=IMSG0003%>,<%=IMSG0004%> "  mustitem = " true " >< br >
26 None.gif < INPUT TYPE = " button "  value = " submit "  onclick = " validate(); return false; " >
27 None.gif < SCRIPT LANGUAGE = " JavaScript " >
28 None.gif <!--
29 None.gif // validate()
30 None.gif // -->
31 None.gif </ SCRIPT >
32 None.gif </ FORM >
33 None.gif </ BODY >
34 None.gif </ HTML >
35 None.gif

validate.js
 1 ExpandedBlockStart.gif ContractedBlock.gif function validate() dot.gif {
 2InBlock.gif
 3InBlock.gif    //var objs = document.all;
 4InBlock.gif    //var Elements = document.getElementsByTagName("*");
 5InBlock.gif    var Elements;
 6InBlock.gif    var ctlArray;
 7InBlock.gif    var ctlArrayName;
 8InBlock.gif    var i, j;
 9InBlock.gif    var elLen, arrLen;
10InBlock.gif    var msgs;
11InBlock.gif
12InBlock.gif    Elements = document.getElementsByTagName("input");
13InBlock.gif    arrName ="";
14InBlock.gif
15ExpandedSubBlockStart.gifContractedSubBlock.gif    for ( i in Elements ) dot.gif{
16InBlock.gif    elLen = Elements[i].length;
17InBlock.gif
18ExpandedSubBlockStart.gifContractedSubBlock.gif        if (elLen > 1dot.gif{
19InBlock.gif            ctlArray = Elements[i];
20InBlock.gif            arrLen = ctlArray.length;
21InBlock.gif
22ExpandedSubBlockStart.gifContractedSubBlock.gif            if (arrName != ctlArray[0].name) dot.gif{
23InBlock.gif                arrName = ctlArray[0].name;
24InBlock.gif
25ExpandedSubBlockStart.gifContractedSubBlock.gif                for (j=0; j < arrLen; j++dot.gif{
26ExpandedSubBlockStart.gifContractedSubBlock.gif                    if (checkValue(ctlArray[j]) == falsedot.gif{
27InBlock.gif                        return false;
28ExpandedSubBlockEnd.gif                    }
 
29ExpandedSubBlockEnd.gif                }

30InBlock.gif
31ExpandedSubBlockEnd.gif            }

32InBlock.gif
33ExpandedSubBlockStart.gifContractedSubBlock.gif        }
elsedot.gif{
34InBlock.gif
35ExpandedSubBlockStart.gifContractedSubBlock.gif            if (checkValue(Elements[i]) == falsedot.gif{
36InBlock.gif                return false;
37ExpandedSubBlockEnd.gif            }

38ExpandedSubBlockEnd.gif        }

39ExpandedSubBlockEnd.gif    }

40InBlock.gif
41InBlock.gif    return true;
42ExpandedBlockEnd.gif}

43 None.gif
44 ExpandedBlockStart.gifContractedBlock.giffunction checkValue (obj) dot.gif {
45InBlock.gif
46InBlock.gif    var len;
47InBlock.gif    len = obj.maxLength;
48InBlock.gif
49ExpandedSubBlockStart.gifContractedSubBlock.gif    if ( obj.type == "text" ) dot.gif{
50InBlock.gif
51InBlock.gif        //if (obj.msg != undefined ) {
52ExpandedSubBlockStart.gifContractedSubBlock.gif        if ( obj.msg ) dot.gif{
53InBlock.gif            msgs = obj.msg.split(",");
54ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else dot.gif{
55InBlock.gif            return true;
56ExpandedSubBlockEnd.gif        }

57InBlock.gif
58ExpandedSubBlockStart.gifContractedSubBlock.gif        if ( trim(obj.value) == "" && obj.mustitem && obj.mustitem == "true"dot.gif{
59InBlock.gif            alert(msgs[0]);
60InBlock.gif            getFocusSelect(obj);
61InBlock.gif            return false;
62ExpandedSubBlockStart.gifContractedSubBlock.gif        }
else dot.gif{
63InBlock.gif
64ExpandedSubBlockStart.gifContractedSubBlock.gif            if ( trim(obj.value) != ""&& obj.feildtype && obj.feildtype == "integer" ) dot.gif{
65InBlock.gif
66ExpandedSubBlockStart.gifContractedSubBlock.gif                if ( checkNumLen(obj.value, len) == false ) dot.gif{
67InBlock.gif                    alert(msgs[1]);
68InBlock.gif                    getFocusSelect(obj);
69InBlock.gif                    return false;
70ExpandedSubBlockEnd.gif                }

71InBlock.gif
72ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 else if ( trim(obj.value) != "" && obj.feildtype && obj.feildtype == "string" ) dot.gif{
73InBlock.gif
74ExpandedSubBlockStart.gifContractedSubBlock.gif                if ( getLenthByByte(trim(obj.value)) > len)dot.gif{
75InBlock.gif                    alert(msgs[1]);
76InBlock.gif                    getFocusSelect(obj);
77InBlock.gif                    return false;
78ExpandedSubBlockEnd.gif                }

79ExpandedSubBlockEnd.gif            }

80ExpandedSubBlockEnd.gif        }

81ExpandedSubBlockEnd.gif    }

82InBlock.gif
83ExpandedBlockEnd.gif}


message.inc
1 None.gif <%
2 None.gifConst IMSG0001  =   " ID必须输入! "
3 None.gifConst IMSG0002  =   " ID输入格式错误! "
4 None.gifConst IMSG0003  =   " 名称必须输入! "
5 None.gifConst IMSG0004  =   " 名称输入格式错误! "
6 None.gif %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值