页面校验通用js.docx
- 文档编号:10571371
- 上传时间:2023-02-21
- 格式:DOCX
- 页数:28
- 大小:21.36KB
页面校验通用js.docx
《页面校验通用js.docx》由会员分享,可在线阅读,更多相关《页面校验通用js.docx(28页珍藏版)》请在冰豆网上搜索。
页面校验通用js
1./******************************************************
2.FileName:
jsLibrary.js
3.Copyright (c) 2003-xxxx *********公司技术开发部
4.Writer:
Nick
5.create Date:
2004-12-28
6.Rewriter:
7.Rewrite Date:
8.Impact:
9.******************************************************/
10.
11./*
12.==================================================================
13.公共函数:
14.功能:
检查是否要显示提示窗口
15.==================================================================
16.*/
17.function IfShow(ShowMsg)
18.{
19.if (ShowMsg == "" )
20.{
21.return false;
22.}
23.else
24.{
25.return true;
26.}
27.
28.}
29.
30.
31./*
32.==================================================================
33.功能:
替换空格(将多个连续空格替换为一个空格)
34.使用:
ReplaceSpace(obj)
35.返回:
string
36.==================================================================
37.*/
38.function ReplaceSpace(str)
39.{
40.while ( str.indexOf(" ") >= 0 )
41.{
42.str = str.replace(" " ," ");
43.}
44.return str;
45.}
46.
47./*
48.==================================================================
49.功能:
非空检查,不忽略空格
50.提示信息:
输入框为空,请输入!
51.使用:
isNull(obj,string)
52.返回:
bool
53.==================================================================
54.*/
55.function isNull(obj,ShowMsg)
56.{
57.var show = IfShow(ShowMsg) ;
58.
59.//非空检查
60.if(obj.value == "")
61.{
62.if (show) alert(ShowMsg);
63.obj.focus();
64.obj.select();
65.return false;
66.}
67.else
68.{
69.return true;
70.}
71.}
72.
73./*
74.==================================================================
75.功能:
邮箱地址检查
76.提示信息:
未输入邮件地址或邮件地址无效!
77.使用:
MailCheck(obj,string)
78.返回:
bool
79.==================================================================
80.*/
81.function MailCheck(obj,ShowMsg)
82.{
83.var show = IfShow(ShowMsg) ;
84.
85.if(obj.value!
= "")
86.{
87.var ok1=obj.value.indexOf("@");
88.var ok2=obj.value.indexOf(".");
89.if(!
((ok1!
=-1)&&(ok2!
=-1)))
90.{
91.if (show) alert(ShowMsg);
92.obj.focus();
93.obj.select();
94.return false;
95.}
96.var allowstrlist = "%<>";
97.var endvalue = true;
98.for (i=0;i 99.{ 100.if (allowstrlist.indexOf(obj.value.substr(i,1))! =-1) 101.{ 102.endvalue=false; 103.break; 104.} 105.} 106.if(endvalue==false) 107.{ 108.if (show) alert(ShowMsg); 109.obj.focus(); 110.obj.select(); 111.return false; 112.} 113.//邮件地址正确 114.return true; 115.} 116.else 117.{ 118.//请输入电子信箱地址 119.if (show) alert(ShowMsg); 120.obj.focus(); 121.obj.select(); 122.return false; 123.} 124.} 125. 126./* 127.================================================================== 128.功能: 检查输入的是否为数字 129.提示信息: 未输入或输入的不是一个合法的数字! 130.使用: isNumeric(obj,string) 131.返回: bool 132.================================================================== 133.*/ 134.function isNumeric(obj,ShowMsg) 135.{ 136.var show = IfShow(ShowMsg) ; 137. 138.var IfTrue = obj.value.search(/^(-|\+)? \d+(\.\d+)? $/) ! = -1; 139. 140.if (show && IfTrue ==false) 141.{ 142.alert(ShowMsg); 143.obj.focus(); 144.obj.select(); 145.return false; 146.} 147.else 148.{ 149.return true; 150.} 151.} 152. 153./* 154.================================================================== 155.功能: 打印 156.使用: Print() 157.返回: 158.================================================================== 159.*/ 160.function Print() 161.{ 162. 163.document.all.print.style.display = "none"; 164.window.print(); 165.window.close(); 166.} 167. 168./* 169.================================================================== 170.功能: 判断是否为日期(格式: yyyy-mm-dd) 171.提示信息: 未输入或输入的日期格式错误! 172.使用: isDate(obj,string) 173.返回: bool 174.================================================================== 175.*/ 176.function isDate(obj,ShowMsg) 177.{ 178.var show = IfShow(ShowMsg) ; 179. 180.if(obj.value==null) 181.{ 182.if (show) alert(ShowMsg); 183.return false; 184.} 185. 186.if(obj.value=="") 187.{ 188.if (show) alert(ShowMsg); 189.return false; 190.} 191. 192.var datePat=/^(\d{2}|\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/; 193. 194.var dateStr=obj.value; 195.// is the format ok? 196.var matchArray = dateStr.match(datePat); 197. 198. 199.if (matchArray==null) 200.{ 201.if (show) alert(ShowMsg); 202.return false; 203.} 204.year=matchArray[1]; 205.month=matchArray[3]; 206.day=matchArray[5]; 207. 208.if (year.length! =4 || month.length! =2 || day.length! =2) 209.{ 210.if (show) alert(ShowMsg); 211.return false; 212.} 213.if (month < 1 || month > 12) 214.{ 215.if (show) alert(ShowMsg); 216.return false; 217.} 218.if (day < 1 || day > 31) 219.{ 220.if (show) alert(ShowMsg); 221.return false; 222.} 223. 224.if ((month==4 || month==6 || month==9 || month==11) && day==31) 225.{ 226.if (show) alert(ShowMsg); 227.return false; 228.} 229. 230.if (month==2) 231.{ 232.var isleap=(year % 4==0 && (year % 100 ! =0 || year % 400==0)); 233.if (day>29 || ((day==29) && (! isleap))) 234.{ 235.if (show) alert(ShowMsg); 236.return false; 237.} 238.} 239.return true; 240.} 241. 242./* 243.================================================================== 244.功能: 日期大小判断(格式: yyyy-mm-dd) 245.提示信息: 未输入或输入的开始日期大于结束日期! 246.使用: JudgeDate(obj1,obj2,string) 247.返回: bool 248.================================================================== 249.*/ 250.function JudgeDate(obj1,obj2,ShowMsg) 251.{ 252.var show = IfShow(ShowMsg) ; 253. 254.var eva = isDate(obj1,"") && isDate(obj2,""); 255. 256.if(obj1.value! ="" && obj2.value! ="" & eva ! = false) 257.{ 258.var date1 = obj1.value; 259.var myDate1 = Date.parse(date1.replace("-","/")); 260.var date2 = obj2.value; 261.var myDate2 = Date.parse(date2.replace("-","/")); 262.if(myDate1 > myDate2) 263.{ 264.if (show) alert(ShowMsg); 265.return false; 266.} 267.else 268.{ 269.return true; 270.} 271.} 272.else 273.{ 274.if (show) alert(ShowMsg); 275.return false; 276.} 277.} 278. 279./* 280.================================================================== 281.功能: 字符串操作,去除字符串两边的空格 282.使用: Trim(string) 283.返回: string 284.================================================================== 285.*/ 286./* 287.================================================================== 288.功能: 去除左边的空格 289.使用: LTrim(string) 290.返回: string 291.================================================================== 292.*/ 293.function LTrim(str) 294.{ 295.var whitespace = new String(" \t\n\r"); 296.var s = new String(str); 297. 298.if (whitespace.indexOf(s.charAt(0)) ! = -1) 299.{ 300.var j=0, i = s.length; 301.while (j < i && whitespace.indexOf(s.charAt(j)) ! = -1) 302.{ 303.j++; 304.} 305.s = s.substring(j, i); 306.} 307.return s; 308.} 309. 310./* 311.================================================================== 312.功能: 去除右边的空格 313.使用: RTrim(string) 314.返回: string 315.================================================================== 316.*/ 317.function RTrim(str) 318.{ 319.var whitespace = new String(" \t\n\r"); 320.var s = new String(str); 321. 322.if (whitespace.indexOf(s.charAt(s.length-1)) ! = -1) 323.{ 324.var i = s.length - 1; 325.while (i >= 0 && whitespace.indexOf(s.charAt(i)) ! = -1) 326.{ 327.i--; 328.} 329.s = s.substring(0, i+1); 330.} 331.return s; 332.} 333. 334.//去除前后空格 335.function Trim(str) 336.{ 337.return RTrim(LTrim(str)); 338.} 339. 340. 341./* 342.================================================================== 343.功能: 无效字符的检测(不允许输入特殊字符) 344.提示信息: 未输入或输入包含非法字符 345.使用: CheckChar(obj,Lchar,string) 346.Lchar: 要检查的特殊字符 347.返回: bool 348.================================================================== 349.*/ 350.function CheckChar(obj,Lchar,ShowMsg) 351.{ 352.var show = IfShow(ShowMsg); 353. 354.var strlist = Lchar; //"~! @#$%^&*? <>\"\'"; 355. 356.//无效字符的检测 357.if(obj.value! = "") 358.{ 359.var tmpbool=true; 360.for (i=0;i 361.{ 362.if(strlist.indexOf(obj.value.substr(i,1))! =-1) 363.{ 364.tmpbool=false; 365.break; 366.} 367.else 368.{} 369.} 370. 371.if(tmpbool==false) 372.{ 373.if (show) alert(ShowMsg + strlist); 374.obj.focus(); 375.obj.select(); 376.return false; 377.} 378.else 379.{ 380.return true; 381.} 382.} 383.else 38
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 页面 校验 通用 js