test.htm
1<!-- #include file="message.inc" -->
2<HTML>
3<TÊTE>
4<TITLE> Nouveau document </TITLE>
5 <meta http-equiv="content-type" content="text/html; charset=shift_jis" />
6 <méta http-equiv="pragma" content="no-cache">
7 <méta http-equiv="cache-control" content="no-cache">
8 <méta http-equiv="expires" content="0">
9<script src="common.js"></script>
10<script src="validate.js"></script>
11
12</HEAD>
13
14<CORPS>
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=" vrai"><br>
26<INPUT TYPE="bouton" value="submit" onclick="validate(); return false;">
27<SCRIPT LANGUAGE="JavaScript">
28<!--
29//valider()
30//-->
31</SCRIPT>
32</FORM>
33</BODY>
34</HTML>
35
valider.js
1fonction valider(){
2
3 //var objs = document.all;
4 //var Elements = document.getElementsByTagName("*");
5 éléments variables ;
6 variables ctlArray ;
7 var ctlArrayName ;
8 var je, j;
9 var elLen, arrLen;
10 messages variables ;
11
12 Éléments = document.getElementsByTagName("input");
13 arrName ="";
14
15 pour ( je dans les éléments ) {
16 elLen = Éléments[i].longueur ;
17
18 si (elLen > 1) {
19 ctlArray = Éléments[i];
20 arrLen = ctlArray.length;
21
22 si (arrName != ctlArray[0].name) {
23 arrName = ctlArray[0].name;
24
25 pour (j=0; j < arrLen; j++) {
26 si (checkValue(ctlArray[j]) == false) {
27 renvoie faux ;
28 }
29 }
30
31 }
32
33 }autrement{
34
35 si (checkValue(Elements[i]) == false) {
36 renvoie faux ;
37 }
38 }
39 }
40
41 renvoie vrai ;
42}
43
44fonction checkValue (obj){
45
46 var lentille;
47 len = obj.maxLength;
48
49 si ( obj.type == "texte" ) {
50
51 //if (obj.msg != non défini ) {
52 si ( obj.msg ) {
53 msgs = obj.msg.split(",");
54 } autre {
55 renvoie vrai ;
56 }
57
58 if ( trim(obj.value) == "" && obj.mustitem && obj.mustitem == "true") {
59 alerte (msgs[0]);
60 getFocusSelect(obj);
61 renvoie faux ;
62 }autre {
63
64 if ( trim(obj.value) != ""&& obj.feildtype && obj.feildtype == "integer" ) {
65
66 si ( checkNumLen(obj.value, len) == false ) {
67 alerte (msgs[1]);
68 getFocusSelect(obj);
69 renvoie faux ;
70 }
71
72 } else if ( trim(obj.value) != "" && obj.feildtype && obj.feildtype == "string" ) {
73
74 si ( getLenthByByte(trim(obj.value)) > len){
75 alerte (msgs[1]);
76 getFocusSelect(obj);
77 renvoie faux ;
78 }
79 }
80 }
81 }
82
83}
message.inc 1<%
2Const IMSG0001 = "ID必须输入!"
3Const IMSG0002 = "ID输入格式错误!"
4Const IMSG0003 = "名称必须输入!"
5Const IMSG0004 = "名称输入格式错误!"
6%>
http://www.cnblogs.com/meil/archive/2007/04/05/700772.html