網頁設計:Javascript實現的自動驗證函式執行個體

來源:互聯網
上載者:User
javascript|函數|設計|網頁|網頁設計

test.htm

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

validate.js

 1function validate(){
 2
 3    //var objs = document.all;
 4    //var Elements = document.getElementsByTagName("*");
 5    var Elements;
 6    var ctlArray;
 7    var ctlArrayName;
 8    var i, j;
 9    var elLen, arrLen;
10    var msgs;
11
12    Elements = document.getElementsByTagName("input");
13    arrName ="";
14
15    for ( i in Elements ) {
16    elLen = Elements[i].length;
17
18        if (elLen > 1) {
19            ctlArray = Elements[i];
20            arrLen = ctlArray.length;
21
22            if (arrName != ctlArray[0].name) {
23                arrName = ctlArray[0].name;
24
25                for (j=0; j < arrLen; j++) {
26                    if (checkValue(ctlArray[j]) == false) {
27                        return false;
28                    }
29                }
30
31            }
32
33        }else{
34
35            if (checkValue(Elements[i]) == false) {
36                return false;
37            }
38        }
39    }
40
41    return true;
42}
43
44function checkValue (obj){
45
46    var len;
47    len = obj.maxLength;
48
49    if ( obj.type == "text" ) {
50
51        //if (obj.msg != undefined ) {
52        if ( obj.msg ) {
53            msgs = obj.msg.split(",");
54        } else {
55            return true;
56        }
57
58        if ( trim(obj.value) == "" && obj.mustitem && obj.mustitem == "true") {
59            alert(msgs[0]);
60            getFocusSelect(obj);
61            return false;
62        }else {
63
64            if ( trim(obj.value) != ""&& obj.feildtype && obj.feildtype == "integer" ) {
65
66                if ( checkNumLen(obj.value, len) == false ) {
67                    alert(msgs[1]);
68                    getFocusSelect(obj);
69                    return false;
70                }
71
72            } else if ( trim(obj.value) != "" && obj.feildtype && obj.feildtype == "string" ) {
73
74                if ( getLenthByByte(trim(obj.value)) > len){
75                    alert(msgs[1]);
76                    getFocusSelect(obj);
77                    return false;
78                }
79            }
80        }
81    }
82
83}

message.inc

1<%
2Const IMSG0001 = "ID必須輸入!"
3Const IMSG0002 = "ID輸入格式錯誤!"
4Const IMSG0003 = "名稱必須輸入!"
5Const IMSG0004 = "名稱輸入格式錯誤!"
6%>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.