1function CheckObjDate(obj)
2{
3 var word = jQuery.trim($(obj).val());
4 var checkResult = CheckDate(word);
5 switch(checkResult)
6 {
7 case 'ERRORNODATE':
8 alert('日付を入力してください。');
9 $(obj).focus();
10 break;
11 case 'ERRORDATEFORMAT':
12 alert('入力した日付の形は間違うしました。確認してください。');
13 $(obj).focus().select();
14 break;
15 default:
16 $(obj).val(checkResult);
17 return true;
18 }
19
20}
21
22/**
23*
24* 機能: 日付検証
25* params:
26* 作成日: 2008/08/04
27* 作成者:
28* 最後更新日:
29* 更新履歴:
30*
31*/
32function CheckDate(checkDate)
33{
34 var reg =/^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\/|-)(?:0?2\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(\/|-)(?:(?:(?:0?[13578]|1[02])\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\2(?:0?[1-9]|1\d|2[0-8]))))$/;
35 var word = jQuery.trim(checkDate);//前、後空レターを削除
36 if(word.length == 0)//入力しない場合
37 {
38 return 'ERRORNODATE';
39 }
40 var splitedWord = word.split("/");//年月日を分割する、対応レター"/"
41 if(splitedWord.length > 3 || splitedWord.length < 2)
42 {
43 return 'ERRORDATEFORMAT';
44 }
45 else
46 {
47 for(var i = 0; i < splitedWord.length; i++)
48 if(splitedWord[i].length == 0)
49 return 'ERRORDATEFORMAT';
50 }
51 if(splitedWord.length == 2)
52 {
53 if(splitedWord[0].length < 1 || splitedWord[0].length > 2)
54 return 'ERRORDATEFORMAT';
55 else if(splitedWord[0].length == 1)
56 {
57 splitedWord[0] = "0" + splitedWord[0];
58 }
59 if(splitedWord[1].length < 1 || splitedWord[1].length > 2)
60 return 'ERRORDATEFORMAT';
61 else if(splitedWord[1].length == 1)
62 {
63 splitedWord[1] = "0" + splitedWord[1];
64 }
65 var now = new Date();
66 word = now.getFullYear() + "/" + splitedWord[0] + "/" + splitedWord[1];
67 }
68 if(splitedWord.length == 3)
69 {
70 if(splitedWord[0].length < 1 || splitedWord[0].length > 4)
71 return 'ERRORDATEFORMAT';
72 else if(splitedWord[0].length == 1)
73 {
74 splitedWord[0] = "200" + splitedWord[0];
75 }
76 else if(splitedWord[0].length == 2)
77 {
78 splitedWord[0] = "20" + splitedWord[0];
79 }
80 if(splitedWord[1].length < 1 || splitedWord[1].length > 2)
81 return 'ERRORDATEFORMAT';
82 else if(splitedWord[1].length == 1)
83 {
84 splitedWord[1] = "0" + splitedWord[1];
85 }
86 if(splitedWord[2].length < 1 || splitedWord[2].length > 2)
87 return 'ERRORDATEFORMAT';
88 else if(splitedWord[2].length == 1)
89 {
90 splitedWord[2] = "0" + splitedWord[2];
91 }
92 word = splitedWord[0] + "/" + splitedWord[1] + "/" + splitedWord[2];
93 }
94 if(!(word.match(reg)))
95 {
96 return 'ERRORDATEFORMAT';
97 }
98 else
99 {
100 return word;
101 }
2{
3 var word = jQuery.trim($(obj).val());
4 var checkResult = CheckDate(word);
5 switch(checkResult)
6 {
7 case 'ERRORNODATE':
8 alert('日付を入力してください。');
9 $(obj).focus();
10 break;
11 case 'ERRORDATEFORMAT':
12 alert('入力した日付の形は間違うしました。確認してください。');
13 $(obj).focus().select();
14 break;
15 default:
16 $(obj).val(checkResult);
17 return true;
18 }
19
20}
21
22/**
23*
24* 機能: 日付検証
25* params:
26* 作成日: 2008/08/04
27* 作成者:
28* 最後更新日:
29* 更新履歴:
30*
31*/
32function CheckDate(checkDate)
33{
34 var reg =/^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\/|-)(?:0?2\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(\/|-)(?:(?:(?:0?[13578]|1[02])\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\2(?:0?[1-9]|1\d|2[0-8]))))$/;
35 var word = jQuery.trim(checkDate);//前、後空レターを削除
36 if(word.length == 0)//入力しない場合
37 {
38 return 'ERRORNODATE';
39 }
40 var splitedWord = word.split("/");//年月日を分割する、対応レター"/"
41 if(splitedWord.length > 3 || splitedWord.length < 2)
42 {
43 return 'ERRORDATEFORMAT';
44 }
45 else
46 {
47 for(var i = 0; i < splitedWord.length; i++)
48 if(splitedWord[i].length == 0)
49 return 'ERRORDATEFORMAT';
50 }
51 if(splitedWord.length == 2)
52 {
53 if(splitedWord[0].length < 1 || splitedWord[0].length > 2)
54 return 'ERRORDATEFORMAT';
55 else if(splitedWord[0].length == 1)
56 {
57 splitedWord[0] = "0" + splitedWord[0];
58 }
59 if(splitedWord[1].length < 1 || splitedWord[1].length > 2)
60 return 'ERRORDATEFORMAT';
61 else if(splitedWord[1].length == 1)
62 {
63 splitedWord[1] = "0" + splitedWord[1];
64 }
65 var now = new Date();
66 word = now.getFullYear() + "/" + splitedWord[0] + "/" + splitedWord[1];
67 }
68 if(splitedWord.length == 3)
69 {
70 if(splitedWord[0].length < 1 || splitedWord[0].length > 4)
71 return 'ERRORDATEFORMAT';
72 else if(splitedWord[0].length == 1)
73 {
74 splitedWord[0] = "200" + splitedWord[0];
75 }
76 else if(splitedWord[0].length == 2)
77 {
78 splitedWord[0] = "20" + splitedWord[0];
79 }
80 if(splitedWord[1].length < 1 || splitedWord[1].length > 2)
81 return 'ERRORDATEFORMAT';
82 else if(splitedWord[1].length == 1)
83 {
84 splitedWord[1] = "0" + splitedWord[1];
85 }
86 if(splitedWord[2].length < 1 || splitedWord[2].length > 2)
87 return 'ERRORDATEFORMAT';
88 else if(splitedWord[2].length == 1)
89 {
90 splitedWord[2] = "0" + splitedWord[2];
91 }
92 word = splitedWord[0] + "/" + splitedWord[1] + "/" + splitedWord[2];
93 }
94 if(!(word.match(reg)))
95 {
96 return 'ERRORDATEFORMAT';
97 }
98 else
99 {
100 return word;
101 }
102}
前台调用代码:<input type="text" onblur="CheckObjDate(this)" />
这里对应了自动加年模式,月日自动补零模式等。想到的地方都尽量对应了,也考虑了闰年的模式,闰年模式的正则表达式是在网上找到的,在此感谢。在此也请大家提一下意见!里面难免有不周到的地方,如果有问题,请大家一定不吝赐教!还有,中间有个别地方是Jquery语法,如果有问题也可以提出来!