JS常用正则表达式

1./* 
2.用途:检查输入的Email信箱格式是否正确   
3.输入:strEmail:字符串   
4.返回:如果通过验证返回true,否则返回false   
5.*/   
6.function checkEmail(strEmail)     
7.{    
8.    //var emailReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;     
9.    var emailReg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;    
10.    if ( emailReg.test(strEmail) ) {            
11.        return true;    
12.    }    
13.    else {    
14.        alert("您输入的Email地址格式不正确!");    
15.        return false;    
16.    }    
17.};    
18.  
19./*  
20. 
21.用途:校验ip地址的格式   
22. 
23.输入:strIP:ip地址   
24. 
25.返回:如果通过验证返回true,否则返回false;   
26. 
27.*/   
28.  
29.function isIP(strIP)     
30.  
31.{    
32.  
33.    if (isNull(strIP)) {    
34.  
35.        return false;    
36.  
37.    }    
38.  
39.    var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g //匹配IP地址的正则表达式     
40.  
41.    if (re.test(strIP)) {    
42.  
43.        if ( RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256) {    
44.  
45.            return true;    
46.  
47.        }    
48.  
49.    }    
50.  
51.    return false;    
52.  
53.};    
54.  
55.     
56.  
57./*   
58. 
59.用途:检查输入手机号码是否正确   
60. 
61.输入:strMobile:字符串   
62. 
63.返回:如果通过验证返回true,否则返回false   
64. 
65.*/   
66.  
67.function checkMobile( strMobile )    
68.  
69.{    
70.  
71.    var regu = /^[1][3][0-9]{9}$/;    
72.  
73.    var re = new RegExp(regu);    
74.  
75.    if (re.test(strMobile)) {    
76.  
77.        return true;    
78.  
79.    }    
80.  
81.    else {    
82.  
83.        return false;    
84.  
85.    }    
86.  
87.};    
88.  
89.     
90.  
91./*   
92. 
93.用途:检查输入的电话号码格式是否正确   
94. 
95.输入:strPhone:字符串   
96. 
97.返回:如果通过验证返回true,否则返回false   
98. 
99.*/   
100.  
101.function checkPhone( strPhone )     
102.  
103.{    
104.  
105.    var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,10}$/;    
106.  
107.    var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/;    
108.  
109.    var prompt = "您输入的电话号码不正确!" if ( strPhone.length > 9 ) {    
110.  
111.        if ( phoneRegWithArea.test(strPhone) ) {    
112.  
113.            return true;    
114.  
115.        }    
116.  
117.        else {    
118.  
119.            alert( prompt );    
120.  
121.            return false;    
122.  
123.        }    
124.  
125.    }    
126.  
127.    else {    
128.  
129.        if ( phoneRegNoArea.test( strPhone ) ) {    
130.  
131.            return true;    
132.  
133.        }    
134.  
135.        else {    
136.  
137.            alert( prompt );    
138.  
139.            return false;    
140.  
141.        }    
142.  
143.    }    
144.  
145.};    
146.  
147.     
148.  
149./*   
150. 
151.用途:检查输入字符串是否为空或者全部都是空格   
152. 
153.输入:str   
154. 
155.返回:如果全是空返回true,否则返回false   
156. 
157.*/   
158.  
159.function isNull( str )    
160.  
161.{    
162.  
163.    if ( str == "" ) {    
164.  
165.        return true;    
166.  
167.    }    
168.  
169.    var regu = "^[ ]+{1}quot;;    
170.  
171.    var re = new RegExp(regu);    
172.  
173.    return re.test(str);    
174.  
175.};    
176.  
177.     
178.  
179./*   
180. 
181.用途:检查输入对象的值是否符合整数格式   
182. 
183.输入:str 输入的字符串   
184. 
185.返回:如果通过验证返回true,否则返回false   
186. 
187.*/   
188.  
189.function isInteger( str )    
190.  
191.{    
192.  
193.    var regu = /^[-]{0,1}[0-9]{1,}$/;    
194.  
195.    return regu.test(str);    
196.  
197.};    
198.  
199.     
200.  
201./*   
202. 
203.用途:检查输入字符串是否符合正整数格式   
204. 
205.输入:s:字符串   
206. 
207.返回:如果通过验证返回true,否则返回false   
208. 
209.*/   
210.  
211.function isNumber( s )    
212.  
213.{    
214.  
215.    var regu = "^[0-9]+{1}quot;;    
216.  
217.    var re = new RegExp(regu);    
218.  
219.    if (s.search(re) != - 1) {    
220.  
221.        return true;    
222.  
223.    }    
224.  
225.    else {    
226.  
227.        return false;    
228.  
229.    }    
230.  
231.};    
232.  
233.     
234.  
235./*   
236. 
237.用途:检查输入字符串是否是带小数的数字格式,可以是负数   
238. 
239.输入:str:字符串   
240. 
241.返回:如果通过验证返回true,否则返回false   
242. 
243.*/   
244.  
245.function isDecimal( str )    
246.  
247.{    
248.  
249.    if (isInteger(str)) {    
250.  
251.        return true;    
252.  
253.    }    
254.  
255.    var re = /^[-]{0,1}(\d+)[\.]+(\d+)$/;    
256.  
257.    if (re.test(str)) {    
258.  
259.        if (RegExp.$1 == 0 && RegExp.$2 == 0) {    
260.  
261.            return false;    
262.  
263.        }    
264.  
265.        return true;    
266.  
267.    }    
268.  
269.    else {    
270.  
271.        return false;    
272.  
273.    }    
274.  
275.};    
276.  
277.     
278.  
279./*   
280. 
281.用途:检查输入对象的值是否符合端口号格式   
282. 
283.输入:str 输入的字符串   
284. 
285.返回:如果通过验证返回true,否则返回false   
286. 
287.*/   
288.  
289.function isPort( str )    
290.  
291.{    
292.  
293.    return (isNumber(str) && str < 65536);    
294.  
295.};    
296.  
297.     
298.  
299./*   
300. 
301.用途:检查输入字符串是否符合金额格式,格式定义为带小数的正数,小数点后最多三位   
302. 
303.输入:s:字符串   
304. 
305.返回:如果通过验证返回true,否则返回false   
306. 
307.*/   
308.  
309.function isMoney( s )    
310.  
311.{    
312.  
313.    var regu = "^[0-9]+[\.][0-9]{0,3}{1}quot;;    
314.  
315.    var re = new RegExp(regu);    
316.  
317.    if (re.test(s)) {    
318.  
319.        return true;    
320.  
321.    }    
322.  
323.    else {    
324.  
325.        return false;    
326.  
327.    }    
328.  
329.};    
330.  
331.     
332.  
333./*   
334. 
335.用途:检查输入字符串是否只由英文字母和数字和下划线组成   
336. 
337.输入:s:字符串   
338. 
339.返回:如果通过验证返回true,否则返回false   
340. 
341.*/   
342.  
343.function isNumberOr_Letter( s )    
344.  
345.{    
346.  
347.    //判断是否是数字或字母     
348.  
349.    var regu = "^[0-9a-zA-Z\_]+{1}quot;;    
350.  
351.    var re = new RegExp(regu);    
352.  
353.    if (re.test(s)) {    
354.  
355.        return true;    
356.  
357.    }    
358.  
359.    else {    
360.  
361.        return false;    
362.  
363.    }    
364.  
365.};    
366.  
367.     
368.  
369./*   
370. 
371.用途:检查输入字符串是否只由英文字母和数字组成   
372. 
373.输入:s:字符串   
374. 
375.返回:如果通过验证返回true,否则返回false   
376. 
377.*/   
378.  
379.function isNumberOrLetter( s )    
380.  
381.{    
382.  
383.    //判断是否是数字或字母     
384.  
385.    var regu = "^[0-9a-zA-Z]+{1}quot;;    
386.  
387.    var re = new RegExp(regu);    
388.  
389.    if (re.test(s)) {    
390.  
391.        return true;    
392.  
393.    }    
394.  
395.    else {    
396.  
397.        return false;    
398.  
399.    }    
400.  
401.};    
402.  
403.     
404.  
405./*   
406. 
407.用途:检查输入字符串是否只由汉字、字母、数字组成   
408. 
409.输入:s:字符串   
410. 
411.返回:如果通过验证返回true,否则返回false   
412. 
413.*/   
414.  
415.function isChinaOrNumbOrLett( s )    
416.  
417.{    
418.  
419.    //判断是否是汉字、字母、数字组成     
420.  
421.    var regu = "^[0-9a-zA-Z\u4e00-\u9fa5]+{1}quot;;    
422.  
423.    var re = new RegExp(regu);    
424.  
425.    if (re.test(s)) {    
426.  
427.        return true;    
428.  
429.    }    
430.  
431.    else {    
432.  
433.        return false;    
434.  
435.    }    
436.  
437.};    
438.  
439.     
440.  
441./*   
442. 
443.用途:判断是否是日期   
444. 
445.输入:date:日期;fmt:日期格式   
446. 
447.返回:如果通过验证返回true,否则返回false   
448. 
449.*/   
450.  
451.function isDate( date, fmt )     
452.  
453.{    
454.  
455.    if (fmt == null) {    
456.  
457.        fmt = "yyyyMMdd";    
458.  
459.    }    
460.  
461.    var yIndex = fmt.indexOf("yyyy");    
462.  
463.    if (yIndex ==- 1) {    
464.  
465.        return false;    
466.  
467.    }    
468.  
469.    var year = date.substring(yIndex, yIndex + 4);    
470.  
471.    var mIndex = fmt.indexOf("MM");    
472.  
473.    if (mIndex ==- 1) {    
474.  
475.        return false;    
476.  
477.    }    
478.  
479.    var month = date.substring(mIndex, mIndex + 2);    
480.  
481.    var dIndex = fmt.indexOf("dd");    
482.  
483.    if (dIndex ==- 1) {    
484.  
485.        return false;    
486.  
487.    }    
488.  
489.    var day = date.substring(dIndex, dIndex + 2);    
490.  
491.    if (!isNumber(year) || year > "2100" || year < "1900") {    
492.  
493.        return false;    
494.  
495.    }    
496.  
497.    if (!isNumber(month) || month > "12" || month < "01") {    
498.  
499.        return false;    
500.  
501.    }    
502.  
503.    if (day > getMaxDay(year, month) || day < "01") {    
504.  
505.        return false;    
506.  
507.    }    
508.  
509.    return true;    
510.  
511.};    
512.  
513.function getMaxDay(year, month)     
514.  
515.{    
516.  
517.    if (month == 4 || month == 6 || month == 9 || month == 11) {    
518.  
519.        return "30";    
520.  
521.    }    
522.  
523.    if (month == 2) {    
524.  
525.        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {    
526.  
527.            return "29";    
528.  
529.        }    
530.  
531.        else {    
532.  
533.            return "28";    
534.  
535.        }    
536.  
537.        return "31";;    
538.  
539.    }    
540.  
541.};    
542.  
543.     
544.  
545./*   
546. 
547.用途:字符1是否以字符串2结束   
548. 
549.输入:str1:字符串;str2:被包含的字符串   
550. 
551.返回:如果通过验证返回true,否则返回false   
552. 
553.*/   
554.  
555.function isLastMatch(str1, str2)     
556.  
557.{    
558.  
559.    var index = str1.lastIndexOf(str2);    
560.  
561.    if (str1.length == index + str2.length) {    
562.  
563.        return true;    
564.  
565.    }    
566.  
567.    return false;    
568.  
569.};    
570.  
571.     
572.  
573./*   
574. 
575.用途:字符1是否以字符串2开始   
576. 
577.输入:str1:字符串;str2:被包含的字符串   
578. 
579.返回:如果通过验证返回true,否则返回false   
580. 
581.*/   
582.  
583.function isFirstMatch(str1, str2)     
584.  
585.{    
586.  
587.    var index = str1.indexOf(str2);    
588.  
589.    if (index == 0) {    
590.  
591.        return true;    
592.  
593.    }    
594.  
595.    return false;    
596.  
597.};    
598.  
599.     
600.  
601./*   
602. 
603.用途:字符1是包含字符串2   
604. 
605.输入:str1:字符串;str2:被包含的字符串   
606. 
607.返回:如果通过验证返回true,否则返回false   
608. 
609.*/   
610.  
611.function isMatch(str1, str2)     
612.  
613.{    
614.  
615.    var index = str1.indexOf(str2);    
616.  
617.    if (index ==- 1) {    
618.  
619.        return false;    
620.  
621.    }    
622.  
623.    return true;    
624.  
625.};    
626.  
627.     
628.  
629./*   
630. 
631.用途:检查输入的起止日期是否正确,规则为两个日期的格式正确,且结束如期>=起始日期   
632. 
633.输入:startDate:起始日期,字符串; endDate:结束如期,字符串   
634. 
635.返回:如果通过验证返回true,否则返回false   
636. 
637.*/   
638.  
639.function checkTwoDate( startDate, endDate )     
640.  
641.{    
642.  
643.    if ( !isDate(startDate) ) {    
644.  
645.        alert("起始日期不正确!");    
646.  
647.        return false;    
648.  
649.    }    
650.  
651.    else if ( !isDate(endDate) ) {    
652.  
653.        alert("终止日期不正确!");    
654.  
655.        return false;    
656.  
657.    }    
658.  
659.    else if ( startDate > endDate ) {    
660.  
661.        alert("起始日期不能大于终止日期!");    
662.  
663.        return false;    
664.  
665.    }    
666.  
667.    return true;    
668.  
669.};    
670.  
671.     
672.  
673./*   
674. 
675.用途:检查复选框被选中的数目   
676. 
677.输入:checkboxID:字符串   
678. 
679.返回:返回该复选框中被选中的数目   
680. 
681.*/   
682.  
683.function checkSelect( checkboxID )     
684.  
685.{    
686.  
687.    var check = 0;    
688.  
689.    var i = 0;    
690.  
691.    if ( document.all(checkboxID).length > 0 )     
692.  
693.    {    
694.  
695.        for ( i = 0; i < document.all(checkboxID).length; i++ ) {    
696.  
697.            if ( document.all(checkboxID).item( i ).checked ) {    
698.  
699.                check += 1;    
700.  
701.            }    
702.  
703.        }    
704.  
705.    }    
706.  
707.    else {    
708.  
709.        if ( document.all(checkboxID).checked ) {    
710.  
711.            check = 1;    
712.  
713.        }    
714.  
715.    }    
716.  
717.    return check;    
718.  
719.}    
720.  
721.function getTotalBytes(varField)     
722.  
723.{    
724.  
725.    if (varField == null) {    
726.  
727.        return - 1;    
728.  
729.    }    
730.  
731.    var totalCount = 0;    
732.  
733.    for (i = 0; i < varField.value.length; i++) {    
734.  
735.        if (varField.value.charCodeAt(i) > 127) {    
736.  
737.            totalCount += 2;    
738.  
739.        }    
740.  
741.        else {    
742.  
743.            totalCount++ ;    
744.  
745.        }    
746.  
747.    }    
748.  
749.    return totalCount;    
750.  
751.}    
752.  
753.function getFirstSelectedValue( checkboxID )    
754.  
755.{    
756.  
757.    var value = null;    
758.  
759.    var i = 0;    
760.  
761.    if ( document.all(checkboxID).length > 0 )    
762.  
763.    {    
764.  
765.        for ( i = 0; i < document.all(checkboxID).length; i++ )    
766.  
767.        {    
768.  
769.            if ( document.all(checkboxID).item( i ).checked ) {    
770.  
771.                value = document.all(checkboxID).item(i).value;    
772.  
773.                break;    
774.  
775.            }    
776.  
777.        }    
778.  
779.    }    
780.  
781.    else {    
782.  
783.        if ( document.all(checkboxID).checked ) {    
784.  
785.            value = document.all(checkboxID).value;    
786.  
787.        }    
788.  
789.    }    
790.  
791.    return value;    
792.  
793.}    
794.  
795.function getFirstSelectedIndex( checkboxID )    
796.  
797.{    
798.  
799.    var value = - 2;    
800.  
801.    var i = 0;    
802.  
803.    if ( document.all(checkboxID).length > 0 )    
804.  
805.    {    
806.  
807.        for ( i = 0; i < document.all(checkboxID).length; i++ ) {    
808.  
809.            if ( document.all(checkboxID).item( i ).checked ) {    
810.  
811.                value = i;    
812.  
813.                break;    
814.  
815.            }    
816.  
817.        }    
818.  
819.    }    
820.  
821.    else {    
822.  
823.        if ( document.all(checkboxID).checked ) {    
824.  
825.            value = - 1;    
826.  
827.        }    
828.  
829.    }    
830.  
831.    return value;    
832.  
833.}    
834.  
835.function selectAll( checkboxID, status )    
836.  
837.{    
838.  
839.    if ( document.all(checkboxID) == null) {    
840.  
841.        return;    
842.  
843.    }    
844.  
845.    if ( document.all(checkboxID).length > 0 )    
846.  
847.    {    
848.  
849.        for ( i = 0; i < document.all(checkboxID).length; i++ ) {    
850.  
851.            document.all(checkboxID).item( i ).checked = status;    
852.  
853.        }    
854.  
855.    }    
856.  
857.    else {    
858.  
859.        document.all(checkboxID).checked = status;    
860.  
861.    }    
862.  
863.}    
864.  
865.function selectInverse( checkboxID )     
866.  
867.{    
868.  
869.    if ( document.all(checkboxID) == null) {    
870.  
871.        return;    
872.  
873.    }    
874.  
875.    if ( document.all(checkboxID).length > 0 )     
876.  
877.    {    
878.  
879.        for ( i = 0; i < document.all(checkboxID).length; i++ )     
880.  
881.        {    
882.  
883.            document.all(checkboxID).item( i ).checked = !document.all(checkboxID).item( i ).checked;    
884.  
885.        }    
886.  
887.    }    
888.  
889.    else {    
890.  
891.        document.all(checkboxID).checked = !document.all(checkboxID).checked;    
892.  
893.    }    
894.  
895.}    
896.  
897.function checkDate( value )     
898.  
899.{    
900.  
901.    if (value == '') {    
902.  
903.        return true;    
904.  
905.    }    
906.  
907.    if (value.length != 8 || !isNumber(value)) {    
908.  
909.        return false;    
910.  
911.    }    
912.  
913.    var year = value.substring(0, 4);    
914.  
915.    if (year > "2100" || year < "1900") {    
916.  
917.        return false;    
918.  
919.    }    
920.  
921.    var month = value.substring(4, 6);    
922.  
923.    if (month > "12" || month < "01") {    
924.  
925.        return false;    
926.  
927.    }    
928.  
929.    var day = value.substring(6, 8);    
930.  
931.    if (day > getMaxDay(year, month) || day < "01") {    
932.  
933.        return false;    
934.  
935.    }    
936.  
937.    return true;    
938.  
939.};    
940.  
941.     
942.  
943./*   
944. 
945.用途:检查输入的起止日期是否正确,规则为两个日期的格式正确或都为空且结束日期>=起始日期   
946. 
947.输入:startDate:起始日期,字符串; endDate: 结束日期,字符串   
948. 
949.返回:如果通过验证返回true,否则返回false   
950. 
951.*/   
952.  
953.function checkPeriod( startDate, endDate )     
954.  
955.{    
956.  
957.    if ( !checkDate(startDate) ) {    
958.  
959.        alert("起始日期不正确!");    
960.  
961.        return false;    
962.  
963.    }    
964.  
965.    else if ( !checkDate(endDate) ) {    
966.  
967.        alert("终止日期不正确!");    
968.  
969.        return false;    
970.  
971.    }    
972.  
973.    else if ( startDate > endDate ) {    
974.  
975.        alert("起始日期不能大于终止日期!");    
976.  
977.        return false;    
978.  
979.    }    
980.  
981.    return true;    
982.  
983.};    
984.  
985.     
986.  
987./*   
988. 
989.用途:检查证券代码是否正确   
990. 
991.输入:secCode:证券代码   
992. 
993.返回:如果通过验证返回true,否则返回false   
994. 
995.*/   
996.  
997.function checkSecCode( secCode )     
998.  
999.{    
1000.  
1001.    if ( secCode.length != 6 ) {    
1002.  
1003.        alert("证券代码长度应该为6位");    
1004.  
1005.        return false;    
1006.  
1007.    }    
1008.  
1009.    if (!isNumber( secCode ) ) {    
1010.  
1011.        alert("证券代码只能包含数字");    
1012.  
1013.        return false;    
1014.  
1015.    }    
1016.  
1017.    return true;    
1018.  
1019.};    
1020.  
1021.     
1022.  
1023./*  
1024. 
1025.function:cTrim(sInputString,iType)   
1026. 
1027.description:字符串去空格的函数   
1028. 
1029.parameters:iType:1=去掉字符串左边的空格;2=去掉字符串左边的空格;0=去掉字符串左边和右边的空格   
1030. 
1031.return value:去掉空格的字符串   
1032. 
1033.*/   
1034.  
1035.function cTrim(sInputString, iType)     
1036.  
1037.{    
1038.  
1039.    var sTmpStr = ' ';    
1040.  
1041.    var i = - 1;    
1042.  
1043.    if (iType == 0 || iType == 1)     
1044.  
1045.    {    
1046.  
1047.        while (sTmpStr == ' ') {    
1048.  
1049.            ++i;    
1050.  
1051.            sTmpStr = sInputString.substr(i, 1);    
1052.  
1053.        }    
1054.  
1055.        sInputString = sInputString.substring(i);    
1056.  
1057.    }    
1058.  
1059.    if (iType == 0 || iType == 2)     
1060.  
1061.    {    
1062.  
1063.        sTmpStr = ' ';    
1064.  
1065.        i = sInputString.length;    
1066.  
1067.        while (sTmpStr == ' ') {    
1068.  
1069.            --i;    
1070.  
1071.            sTmpStr = sInputString.substr(i, 1);    
1072.  
1073.        }    
1074.  
1075.        sInputString = sInputString.substring(0, i + 1);    
1076.  
1077.    }    
1078.  
1079.    return sInputString;    
1080.  
1081.};   



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值