现在的位置: 首页 > 综合 > 正文

常用的一些javascript代码

2013年11月12日 ⁄ 综合 ⁄ 共 7808字 ⁄ 字号 评论关闭
1.// 是否为空,非空返回真,不非为空返回假   
2.function isBlank(str) {   
3.    var blankFlag = true;   
4.    if (str.length == 0) return true;   
5.    for (var i = 0; i < str.length; i++) {   
6.        if ((str.charAt(i) != "") && (str.charAt(i) != " ")) {   
7.            blankFlag = false;   
8.            break;   
9.        }   
10.    }   
11.    return blankFlag;   
12.}   
13.function checkNotNull(theField, fieldName) {   
14.    if(isBlank(theField.value)){   
15.        alert(fieldName + "不可为空!");   
16.                if(theField.type!="hidden"){   
17.          theField.focus();   
18.                }   
19.        return false;   
20.    }   
21.    return true;   
22.}   
23.  
24.// 是否为数字   
25.function checkNumber(theField, fieldName) {   
26.     var pattern = /^([0-9]|(-[0-9]))[0-9]*((\.[0-9]+)|([0-9]*))$/;   
27.     if(theField.value.trim() == "") return true;   
28.     if (!pattern.test(theField.value.trim())) {   
29.         alert(fieldName + "必须为合法数字");   
30.         theField.focus();   
31.         theField.select();   
32.         return false;   
33.     }   
34.  
35.    return true;   
36.}   
37.  
38.function isNumber(str) {   
39.  var pattern = /^([0-9]|(-[0-9]))[0-9]*((\.[0-9]+)|([0-9]*))$/;   
40.  if(str.trim() == "") return false;   
41.  if (!pattern.test(str.trim())) return false;   
42.  return true;   
43.}   
44.  
45.// 是否为指定范围数字   
46.function checkNumberRange(theField, fieldName, min, max) {   
47.    if(theField.value.trim() == "") return true;   
48.    if (!checkNumber(theField, fieldName)) return false;   
49.  
50.    if ((min != "") && (theField.value.trim() < min)) {   
51.        alert(fieldName + "不可小于" + min + "!");   
52.        theField.focus();   
53.        theField.select();   
54.        return false;   
55.    }   
56.  
57.    if ((max != "") && (theField.value.trim() > max)) {   
58.        alert(fieldName + "不可超过" + max + "!");   
59.        theField.focus();   
60.        theField.select();   
61.        return false;   
62.    }   
63.  
64.    return true;   
65.}   
66.  
67.function isNumberRange(str, min, max) {   
68.if(str == "") return false;   
69.    if (!isNumber(str)) return false;   
70.  
71.    if ((min != "") && (str < min)) {   
72.        return false;   
73.    }   
74.  
75.    if ((max != "") && (str > max)) {   
76.        return false;   
77.    }   
78.  
79.    return true;   
80.}   
81.  
82.// 是否为整数   
83.function checkInteger(theField, fieldName) {   
84.    var pattern = /^(\d|(-\d))\d*$/;   
85.  
86.    if(theField.value.trim() == "") return true;   
87.    if (!pattern.test(theField.value.trim())) {   
88.        alert(fieldName + "必须为整数!");   
89.        theField.focus();   
90.        theField.select();   
91.        return false;   
92.    }   
93.  
94.    return true;   
95.}   
96.  
97.function isInteger(str) {   
98.    var pattern = /^(\d|(-\d))\d*$/;   
99.  
100.    if(str.trim() == "") return false;   
101.    if (!pattern.test(str.trim())) {   
102.        return false;   
103.    }   
104.    return true;   
105.}   
106.  
107.// 是否为指定范围内整数   
108.function checkIntegerRange(theField, fieldName, min, max) {   
109.    if(theField.value.trim() == "") return true;   
110.    if (!checkInteger(theField, fieldName)) return false;   
111.  
112.    if ((min != "") && (theField.value.trim() < min)) {   
113.        alert(fieldName + "不可小于" + min + "!");   
114.        theField.focus();   
115.        theField.select();   
116.        return false;   
117.    }   
118.  
119.    if ((max != "") && (theField.value.trim() > max)) {   
120.        alert(fieldName + "不可超过" + max + "!");   
121.        theField.focus();   
122.        theField.select();   
123.        return false;   
124.    }   
125.  
126.    return true;   
127.}   
128.  
129.function isIntegerRange(str, min, max) {   
130.    if(str == "") return false;   
131.    if (!isInteger(str)) return false;   
132.  
133.    if ((min != "") && (str < min)) {   
134.        return false;   
135.    }   
136.  
137.    if ((max != "") && (str > max)) {   
138.        return false;   
139.    }   
140.  
141.    return true;   
142.}   
143.  
144.// 是否为正数   
145.function checkPositiveNumber(theField, fieldName) {   
146.    if(theField.value == "") return true;   
147.    if (theField.value.charAt(0) == '-') {   
148.        alert(fieldName + "必须为正数!");   
149.        theField.focus();   
150.        return false;   
151.    }   
152.  
153.    return true;   
154.}  

 

1.// 是否规定的浮点数,包括小数点前的位数和小数点后的位数,beforeDec是小数点前的位数,afterDec是小数点后的为数   
2.function checkDecimalNumber(theField, fieldName, beforeDec, afterDec){   
3.  if (theField.value == "") return true;   
4.  var pattern ="/^(|[+-]?(0|([1-9]\\d{0,"+(beforeDec-1)+"})|((0|([1-9]\\d{0,"+(beforeDec-1)+"}))?\\.\\d{1,"+afterDec+"})){1,1})$/";   
5.  if (!eval(pattern).test(theField.value)) {   
6.     alert(fieldName + "必须为合法数字,允许小数点前"+beforeDec+"位和小数点后"+afterDec+"位之内");   
7.     theField.focus();   
8.     theField.select();   
9.     return false;   
10.  }   
11.  return true;   
12.}   
13.  
14.//使用 prototype 为 String 对象增加一个新方法 length2,找出字符串中有多少个汉字,然后将这个数字加上 length 的值就是我们想要的结果了。   
15.String.prototype.length2 = function() {   
16.    var cArr = this.match(/[^\x00-\xff]/ig);   
17.    return this.length + (cArr == null ? 0 : cArr.length);   
18.}   
19.// 限制字串最大长度   
20.function checkLength(theField, fieldName, maxLength) {   
21.    if(theField.value == "") return true;   
22.    if (theField.value.length2() > maxLength) {   
23.        alert(fieldName + "的字数最多为" + maxLength + "字!");   
24.        theField.select();   
25.        theField.focus();   
26.        return false;   
27.    }   
28.  
29.    return true;   
30.}   
31.  
32.// 限制字串长度,注意参数顺序   
33.function checkLength2(theField, fieldName, maxLength, minLength) {   
34.    if(theField.value == "") return true;   
35.    if (theField.value.length2() > maxLength) {   
36.        alert(fieldName + "的字数最多为" + maxLength + "字!");   
37.        theField.focus();   
38.        return false;   
39.    }   
40.  
41.    if ((minLength != "") && (theField.value.length2() < minLength)) {   
42.        alert(fieldName + "的字数最少为" + minLength + "字!");   
43.        theField.focus();   
44.        return false;   
45.    }   
46.  
47.    return true;   
48.}   
49.  
50.// 所输入字符串是否均为合法字符   
51.// charBag中为包含所有合法字符的字符串   
52.function checkStrLegal(theField, fieldName, charBag) {   
53.    if(theField.value == "") return true;   
54.       for (var i = 0; i < theField.value.length; i++) {   
55.        var c = theField.value.charAt(i);   
56.        if (charBag.indexOf(c) == -1) {   
57.             alert(fieldName + "含有非法字符(" + c + ")!");   
58.             theField.focus();   
59.             return false;   
60.        }   
61.    }   
62.  
63.    return true;   
64.}   
65.  
66.// 所输入字符串是否均为合法字符   
67.// charBag中为包含非法字符的字符串   
68.function checkStrLegal2(theField, fieldName, charBag) {   
69.    if(theField.value == "") return true;   
70.       for (var i = 0; i < theField.value.length; i++) {   
71.        var c = theField.value.charAt(i);   
72.        if (charBag.indexOf(c) > -1) {   
73.             alert(fieldName + "含有非法字符(" + c +")!");   
74.             theField.focus();   
75.             return false;   
76.        }   
77.    }   
78.  
79.    return true;   
80.}  

 

1.// 电子邮件验证   
2.function checkEmail(theField) {   
3.    var pattern = /^.+@.+\..+$/;   
4.  
5.    if(theField.value == "") return true;   
6.    if (!pattern.test(theField.value)) {   
7.        alert("请输入合法的电子邮件地址");   
8.        theField.focus();   
9.        theField.select();   
10.        return false;   
11.    }   
12.  
13.    return true;   
14.}   
15.  
16.// 是否为只读域(如file,text等域只接受右边按钮选择传回的结果)   
17.function checkReadField() {   
18.    alert("请点击后面的图标进行选择");   
19.    // this.blur();   
20.}   
21./*  
22.*    RoundTo(Digit,How):数值格式化函数,Digit要格式化的数字,How要保留的小数位数。  
23.*/  
24.function  RoundTo(Digit,How)   
25.{   
26.   Digit  =  Math.round(Digit*Math.pow(10,How))/Math.pow(10,How);   
27.   return  Digit;   
28.}   
29.//去除字符串的前后空格   
30.String.prototype.trim = function()   
31.{   
32.   return this.replace(/(^\s+)|\s+$/g,"");   
33.}   
34.  
35.//将指定的字段转换为大写   
36.function UpperCase(theField){   
37.  theField.value = theField.value.toUpperCase();   
38.}   
39.  
40.//将指定的字段转换为小写   
41.function LowerCase(theField){   
42.  theField.value = theField.value.toLowerCase();   
43.}   
44.  
45.//比较两个时间大小,相等或第二个大返回true,第一个大返回false.   
46.//现在只支持2005-12-30或2005-12-30 10:00:00的时间格式   
47.function DateCompare(theField1,fieldName1,theField2,fieldName2)   
48.{   
49.  var d1 = theField1.value;   
50.  var d2 = theField2.value;   
51.  if (d1==d2) return true;    // 相等   
52.  var a1 = null;var a2 = null;   
53.  var b1 = null;var b2 = null;   
54.  if (d1.length==10){   
55.    d1 += " 00:00:00";   
56.  }   
57.  a1=d1.substring(0,10).split("-");   
58.  a2=d1.substring(11).split(":");   
59.  if (d2.length==10){   
60.    d2 += " 00:00:00";   
61.  }   
62.  b1=d2.substring(0,10).split("-");   
63.  b2=d2.substring(11).split(":");   
64.  
65.  for (i=0;i<3;i++){   
66.    if( a1[i].charAt(0) == '0' ) { a1[i] = a1[i].substring(1,2); }   
67.    if( b1[i].charAt(0) == '0' ) { b1[i] = b1[i].substring(1,2); }   
68.    if (parseInt(a1[i])>parseInt(b1[i])){   
69.      alert(fieldName1+"不能大于"+fieldName2);   
70.      return false;   
71.    }   
72.    if (parseInt(a1[i])<parseInt(b1[i])){   
73.      return true;   
74.    }   
75.  }   
76.  for (i=0;i<3;i++){   
77.    if( a2[i].charAt(0) == '0' ) { a2[i] = a2[i].substring(1,2); }   
78.    if( b2[i].charAt(0) == '0' ) { b2[i] = b2[i].substring(1,2); }   
79.    if (parseInt(a2[i])>parseInt(b2[i])){   
80.      alert(fieldName1+"不能大于"+fieldName2);   
81.      return false;   
82.    }   
83.    if (parseInt(a2[i])<parseInt(b2[i])){   
84.      return true;   
85.    }   
86.  }   
87.}   
88.  
89.//将给定的字符串中的&字符替换成@@   
90.function ConvertStr(s){   
91.  var i;   
92.  var s2 = s;   
93.  
94.  while(s2.indexOf("&")>0){   
95.     i = s2.indexOf("&");   
96.     s2 = s2.substring(0, i) + "@@" + s2.substring(i + 1, s2.length);   
97.  }   
98.  return s2;   
99.}   
100.  
101.//将给定的字符串中的@@字符替换成&   
102.function ReConvertStr(s){   
103.  var i;   
104.  var s2 = s;   
105.  
106.  while(s2.indexOf("@@")>0){   
107.     i = s2.indexOf("@@");   
108.     s2 = s2.substring(0, i) + "&" + s2.substring(i + 2, s2.length);   
109.  }   
110.  return s2;   
111.}   
112.//将给定的字符串中的单双引号转义   
113.function ForamtValue(oStr)   
114.{   
115.    switch(typeof(oStr))   
116.    {   
117.        case "date"     :   
118.            //直接toString()转换,可以加入丰富的显示方式   
119.            sStr = (new Date(oStr)).toString();   
120.            break;   
121.        default         :   
122.            sStr = String(oStr);   
123.    }   
124.    sStr = sStr.replace(/\"/g,"""); //输入框中显示双引号问题   
125.    sStr = sStr.replace(/\'/g,"'"); //输入框中显示单引号问题   
126.    return sStr;   
127.}  

 

抱歉!评论已关闭.