Web Design: an example of automatic validation function for JavaScript implementation

Source: Internet
Author: User
Tags format integer return trim
javascript| Function | design | Web page design

Test.htm

1<!--#include file= "Message.inc"-->
234<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
1213
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 name <input type= "text" id= "txtstring" name= "txtstring" maxlength= "5" feildtype= "string" msg= "&LT;%=IMSG0003%&GT; <%=IMSG0004%> "mustitem=" true ><br>
22 name <input type= "text" id= "txtstring" name= "txtstring" maxlength= "5" feildtype= "string" msg= "&LT;%=IMSG0003%&GT; <%=IMSG0004%> "mustitem=" true ><br>
23 name <input type= "text" id= "txtstring" name= "txtstring" maxlength= "5" feildtype= "string" msg= "&LT;%=IMSG0003%&GT; <%=IMSG0004%> "mustitem=" true ><br>
24
25 name 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>
3435

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;
Ten Var msgs;
11
Elements = document.getElementsByTagName ("input");
Arrname = "";
14
For (i in Elements) {
ElLen = Elements[i].length;
17
if (ElLen > 1) {
Ctlarray = Elements[i];
Arrlen = Ctlarray.length;
21st
if (arrname!= ctlarray[0].name) {
Arrname = Ctlarray[0].name;
24
for (J=0 J < Arrlen; J + +) {
if (CheckValue (ctlarray[j]) = = False) {
return false;
28}
29}
30
31}
32
}else{
34
if (CheckValue (elements[i]) = = False) {
return false;
37}
38}
39}
40
The return is true;
42}
43
44function CheckValue (obj) {
45
the Var Len;
len = obj.maxlength;
48
if (Obj.type = = "Text") {
50
Wuyi//if (obj.msg!= undefined) {
if (obj.msg) {
Msgs = Obj.msg.split (",");
' Else {
The return is true;
56}
57
(Trim (obj.value) = = "" && obj.mustitem && Obj.mustitem = = "true") {
Alert (msgs[0]);
Getfocusselect (obj);
return false;
}else {
63
if (Trim (obj.value)!= "" && obj.feildtype && Obj.feildtype = = "Integer") {
65
if (Checknumlen (obj.value, len) = = False) {
Alert (msgs[1]);
Getfocusselect (obj);
return false;
70}
71
' Else if ' (Trim (obj.value)!= "" && obj.feildtype && Obj.feildtype = = "string") {
73
Getlenthbybyte (Trim (obj.value) > Len) {
Alert (msgs[1]);
Getfocusselect (obj);
The return false;
78}
79}
80}
81}
82
83}

Message.inc

1<%
2Const IMSG0001 = "ID must be entered! "
3Const IMSG0002 = "id input format Error!" "
4Const IMSG0003 = "Name must be entered!" "
5Const IMSG0004 = "Name Input format Error!" "
6%>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.