您的位置:首页 > Web前端 > JavaScript

2008年总结项目中常用到的JS验证脚本

2009-08-05 18:49 537 查看
1 // ================================================================
2 // 本函数用于限制文本输入框中只能输入数字"0"到"9",".","-"
3 // ================================================================
4 function JHshNumberText()
5 {
6 if ( !(((window.event.keyCode >= 48) && (window.event.keyCode <= 57))
7 || (window.event.keyCode == 13) || (window.event.keyCode == 46)
8 || (window.event.keyCode == 45)))
9 {
10 window.event.keyCode = 0 ;
11 }
12 //<form name=frm>
13 //<input type=text name=test value="" onKeypress="JHshNumberText()">
14 //<input type=button name=submit value=submit>
15 //</form>
16 }
17
18 // ================================================================
19 // 本函数用于自动将输入文本框中的内容转换成大写字符
20 // ================================================================
21 function JHshToUpperCase()
22 {
23 if ((window.event.keyCode >= 97) && (window.event.keyCode <= 122))
24 {
25 window.event.keyCode = window.event.keyCode - 32 ;
26 }
27 }
28
29 // ================================================================
30 // 本函数用于自动将输入文本框中的内容转换成小写字符
31 // ================================================================
32 function JHshToLowerCase()
33 {
34 if ((window.event.keyCode >= 65) && (window.event.keyCode <= 90))
35 {
36 window.event.keyCode = window.event.keyCode + 32 ;
37 }
38 }
39
40 ///截除字符串前后空格
41 function JHshTrim(sString)
42 {
43 var strTmp ;
44 strTmp = JHshRTrim(JHshLTrim(sString)) ;
45 return strTmp ;
46 }
47
48 // -----------------------------------------------------------------------------------
49 //本函数用于对sString字符串进行前空格截除
50 // -----------------------------------------------------------------------------------
51 function JHshLTrim(sString)
52 {
53 var sStr,i,iStart,sResult = "";
54
55 sStr = sString.split("");
56 iStart = -1 ;
57 for (i = 0 ; i < sStr.length ; i++)
58 {
59 if (sStr[i] != " ")
60 {
61 iStart = i;
62 break;
63 }
64 }
65 if (iStart == -1)
66 { return "" ;} //表示sString中的所有字符均是空格,则返回空串
67 else
68 { return sString.substring(iStart) ;}
69 }
70
71 // -----------------------------------------------------------------------------------
72 //本函数用于对sString字符串进行后空格截除
73 // -----------------------------------------------------------------------------------
74 function JHshRTrim(sString)
75 {
76 var sStr,i,iStart,sResult = "";
77 sStr = sString.split("");
78 iStart = -1 ;
79 for (i = sStr.length-1 ; i >= 0 ; i--)
80 {
81 if (sStr[i] != " ")
82 {
83 iStart = i;
84 break;
85 }
86 }
87
88 if (iStart == -1)
89 { return "" ;} //表示sString中的所有字符均是空格,则返回空串
90 else
91 { return sString.substring(0,iStart+1) ;}
92 }
93
94
95 //------------------------------------------------------------------------------------
96 //判断字符串是否是日期格式,//标准格式:2004-3-29 12:05 其他格式都不允许
97 //------------------------------------------------------------------------------------
98 function IsDate(DateString)
99 {
100 if( DateString == null )
101 return false;
102
103 var Dilimeter = '-';
104 var tempY = '';
105 var tempM = '';
106 var tempD = '';
107
108 var tempH = '';
109 var tempMi = '';
110 var tempS = '';
111
112 var tempArr1,tempArr2,tempArr3;
113 if( DateString.length < 8 && DateString.length > 18 )
114 return false;
115 tempArr1 = DateString.split(" ");
116 if( tempArr1.length != 2)
117 return false;
118
119 tempArr2 = tempArr1[0].split(Dilimeter);
120 if( tempArr2.length != 3 )
121 return false;
122 tempY = tempArr2[0];
123 tempM = tempArr2[1];
124 tempD = tempArr2[2];
125
126 tempArr3 = tempArr1[1].split(':');
127 if( tempArr2.length < 2 || tempArr2.length > 3)
128 {
129 return false;
130 }
131 tempH = tempArr3[0];
132 tempMi = tempArr3[1];
133 if( tempArr2.length == 3)
134 tempS = tempArr3[2];
135
136 var tDateString;
137 if( tempArr3.length == 2)
138 {
139 tDateString = tempY + '/' + tempM +'/' +tempD +' '+tempH+':'+tempMi;
140 }
141 else
142 {
143 tDateString = tempY + '/' + tempM +'/' +tempD +' '+tempH+':'+tempMi+":"+tempS;
144 }
145 var tempDate = new Date( tDateString );
146 if( isNaN(tempDate))
147 {
148 //alert("isNAN");
149 return false;
150 }
151
152 if( tempArr3.length == 2)
153 {
154 //2004-3-29 12:05
155 if((tempDate.getUTCFullYear().toString() == tempY) &&
156 (tempDate.getMonth() == parseInt(tempM)-1) &&
157 (tempDate.getDate() == parseInt(tempD)) &&
158 (tempDate.getHours() == parseInt(tempH)) &&
159 (tempDate.getMinutes() == parseInt(tempMi)))
160 {
161 return true;
162 }
163 else
164 {
165 return false;
166 }
167 }
168 else
169 {
170 //2004-3-29 12:05:30
171 if((tempDate.getUTCFullYear().toString() == tempY) &&
172 (tempDate.getMonth() == parseInt(tempM)-1) &&
173 (tempDate.getDate() == parseInt(tempD)) &&
174 (tempDate.getHours() == parseInt(tempH)) &&
175 (tempDate.getMinutes() == parseInt(tempMi)) &&
176 (tempDate.getSeconds() == parseInt(tempS)))
177 {
178 return true;
179 }
180 else
181 {
182 return false;
183 }
184 }
185
186 }
187
188 //------------------------------------------------------------------------------------
189 //判断字符串日期的大小,//标准格式:2004-3-29 12:05
190 // 用之前判断日期的有效性
191 // 如果DateString1比DateString2时间晚,则返回true;否则false
192 //------------------------------------------------------------------------------------
193
194 function DateCompare(DateString1, DateString2)
195 {
196 var i;
197 var Dilimeter = '-';
198
199 var tempArr1,tempArr2;
200 tempArr1 = DateString1.split(Dilimeter);
201 tempArr2 = DateString2.split(Dilimeter);
202
203 var tDateString1 = "";
204 for(i=0; i<tempArr1.length; i++)
205 {
206 if( i < tempArr1.length-1)
207 tDateString1 += tempArr1[i]+"/";
208 else
209 tDateString1 += tempArr1[i];
210 }
211 var tDateString2 = "";
212 for(i=0; i<tempArr2.length; i++)
213 {
214 if( i < tempArr2.length-1)
215 tDateString2 += tempArr2[i]+"/";
216 else
217 tDateString2 += tempArr2[i];
218
219 }
220
221 var tempDate1 = new Date( tDateString1 );
222 var tempDate2 = new Date( tDateString2 );
223 if( isNaN(tempDate1) || isNaN(tempDate2))
224 {
225 return false;
226 }
227
228 //2004-3-29 12:05:30
229 if( tempDate1.getUTCFullYear() < tempDate2.getUTCFullYear())
230 return false;
231 if( tempDate1.getUTCFullYear() > tempDate2.getUTCFullYear())
232 return true;
233 if( tempDate1.getMonth() < tempDate2.getMonth())
234 return false;
235 if( tempDate1.getMonth() > tempDate2.getMonth())
236 return true;
237 if( tempDate1.getDate() < tempDate2.getDate())
238 return false;
239 if( tempDate1.getDate() > tempDate2.getDate())
240 return true;
241 if( tempDate1.getHours() < tempDate2.getHours())
242 return false;
243 if( tempDate1.getHours() > tempDate2.getHours())
244 return true;
245 if( tempDate1.getMinutes() < tempDate2.getMinutes())
246 return false;
247 if( tempDate1.getMinutes() > tempDate2.getMinutes())
248 return true;
249 if( tempDate1.getSeconds() < tempDate2.getSeconds())
250 return false;
251 if( tempDate1.getSeconds() > tempDate2.getSeconds())
252 return true;
253 //如果相等则返回false
254 return false;
255 }
256
257 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liuliang1232005/archive/2008/05/24/2476222.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: