📄 用javascript判断日期、数字、整数和特殊字符 - 东猫北熊的技术积累 - 博客园.mht
字号:
#000000"> ,sign)<BR>{ <BR>var integer;<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000"> ((sign</SPAN><SPAN=20
style=3D"COLOR: #000000">!=3D</SPAN><SPAN=20
style=3D"COLOR: #0000ff">null</SPAN><SPAN=20
style=3D"COLOR: #000000">) </SPAN><SPAN=20
style=3D"COLOR: #000000">&&</SPAN><SPAN=20
style=3D"COLOR: #000000"> (sign</SPAN><SPAN=20
style=3D"COLOR: #000000">!=3D</SPAN><SPAN style=3D"COLOR: =
#000000">=E2=80=B2</SPAN><SPAN=20
style=3D"COLOR: #000000">-</SPAN><SPAN=20
style=3D"COLOR: #000000">=E2=80=B2) </SPAN><SPAN=20
style=3D"COLOR: #000000">&&</SPAN><SPAN=20
style=3D"COLOR: #000000"> (sign</SPAN><SPAN=20
style=3D"COLOR: #000000">!=3D</SPAN><SPAN style=3D"COLOR: =
#000000">=E2=80=B2</SPAN><SPAN=20
style=3D"COLOR: #000000">+</SPAN><SPAN=20
style=3D"COLOR: =
#000000">=E2=80=B2))<BR>{<BR>alert(=E2=80=B2IsInter(</SPAN><SPAN=20
style=3D"COLOR: #0000ff">string</SPAN><SPAN=20
style=3D"COLOR: =
#000000">,sign)=E7=9A=84=E5=8F=82=E6=95=B0=E5=87=BA=E9=94=99=EF=BC=9A&nbs=
p;sign=E4=B8=BAnull=E6=88=96</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN style=3D"COLOR: =
#000000">-</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN style=3D"COLOR: =
#000000">=E6=88=96</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN style=3D"COLOR: =
#000000">+</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: #000000">=E2=80=B2);<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">false</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR>}<BR>integer </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> parseInt(</SPAN><SPAN=20
style=3D"COLOR: #0000ff">string</SPAN><SPAN=20
style=3D"COLOR: #000000">);<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> (isNaN(integer))<BR>{<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">false</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR>}<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">else</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> (integer.toString().length</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN=20
style=3D"COLOR: #0000ff">string</SPAN><SPAN=20
style=3D"COLOR: #000000">.length)<BR>{ <BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000"> ((sign</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN=20
style=3D"COLOR: #0000ff">null</SPAN><SPAN=20
style=3D"COLOR: #000000">) </SPAN><SPAN=20
style=3D"COLOR: #000000">||</SPAN><SPAN=20
style=3D"COLOR: #000000"> (sign</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN style=3D"COLOR: =
#000000">=E2=80=B2</SPAN><SPAN=20
style=3D"COLOR: #000000">-</SPAN><SPAN=20
style=3D"COLOR: #000000">=E2=80=B2 </SPAN><SPAN=20
style=3D"COLOR: #000000">&&</SPAN><SPAN=20
style=3D"COLOR: #000000"> integer</SPAN><SPAN=20
style=3D"COLOR: #000000"><</SPAN><SPAN=20
style=3D"COLOR: #000000">0</SPAN><SPAN=20
style=3D"COLOR: #000000">) </SPAN><SPAN=20
style=3D"COLOR: #000000">||</SPAN><SPAN=20
style=3D"COLOR: #000000"> (sign</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN style=3D"COLOR: =
#000000">=E2=80=B2</SPAN><SPAN=20
style=3D"COLOR: #000000">+</SPAN><SPAN=20
style=3D"COLOR: #000000">=E2=80=B2 </SPAN><SPAN=20
style=3D"COLOR: #000000">&&</SPAN><SPAN=20
style=3D"COLOR: #000000"> integer</SPAN><SPAN=20
style=3D"COLOR: #000000">></SPAN><SPAN=20
style=3D"COLOR: #000000">0</SPAN><SPAN=20
style=3D"COLOR: #000000">))<BR>{<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">true</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR>}<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">else</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">false</SPAN><SPAN=20
style=3D"COLOR: #000000">; <BR>}<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">else</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">false</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR>}<BR><BR></SPAN><SPAN=20
style=3D"COLOR: #008000">/*</SPAN><SPAN=20
style=3D"COLOR: =
#008000">*<BR>IsDate: =E7=94=A8=E4=BA=8E=E5=88=A4=E6=96=AD=E4=B8=80=E4=
=B8=AA=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=98=AF=E5=90=A6=E6=98=AF=E6=97=A5=E6=9C=
=9F=E6=A0=BC=E5=BC=8F=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2<BR><BR>=E8=BF=94=
=E5=9B=9E=E5=80=BC=EF=BC=9A<BR>true=E6=88=96false<BR><BR>=E5=8F=82=E6=95=B0=
=EF=BC=9A<BR>DateString=EF=BC=9A =E9=9C=80=E8=A6=81=E5=88=A4=E6=96=AD=
=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2<BR>Dilimeter =EF=BC=9A =E6=
=97=A5=E6=9C=9F=E7=9A=84=E5=88=86=E9=9A=94=E7=AC=A6=EF=BC=8C=E7=BC=BA=E7=9C=
=81=E5=80=BC=E4=B8=BA=E2=80=B2-=E2=80=B2<BR><BR>Author: PPDJ<BR>samp=
le:<BR>var date =3D =E2=80=B21999-1-2=E2=80=B2;<BR>if =
;(IsDate(date))<BR>{<BR>alert(=E2=80=B2You see, the defaul=
t separator is "-");<BR>}<BR>date =3D =E2=80=B21=
999/1/2";<BR>if (IsDate(date,=E2=80=B2/=E2=80=B2))<BR>{<BR>alert(=E2=
=80=B2The date=E2=80=B2s separator is "/");<BR>}<BR><=
/SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: =
#000000"><BR><BR>function IsDate(DateString , Dilimeter)<B=
R>{<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000"> (DateString</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN=20
style=3D"COLOR: #0000ff">null</SPAN><SPAN=20
style=3D"COLOR: #000000">) </SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">false</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000"> (Dilimeter</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN=20
style=3D"COLOR: #000000">=E2=80=B2=E2=80=B2 </SPAN><SPAN=20
style=3D"COLOR: #000000">||</SPAN><SPAN=20
style=3D"COLOR: #000000"> Dilimeter</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN=20
style=3D"COLOR: #0000ff">null</SPAN><SPAN=20
style=3D"COLOR: #000000">)<BR>Dilimeter </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> =E2=80=B2</SPAN><SPAN=20
style=3D"COLOR: #000000">-</SPAN><SPAN=20
style=3D"COLOR: #000000">=E2=80=B2;<BR>var tempy</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000">=E2=80=B2=E2=80=B2;<BR>var tempm</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000">=E2=80=B2=E2=80=B2;<BR>var tempd</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000">=E2=80=B2=E2=80=B2;<BR>var tempArray;<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000"> (DateString.length</SPAN><SPAN=20
style=3D"COLOR: #000000"><</SPAN><SPAN=20
style=3D"COLOR: #000000">8</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #000000">&&</SPAN><SPAN=20
style=3D"COLOR: #000000"> DateString.length</SPAN><SPAN=20
style=3D"COLOR: #000000">></SPAN><SPAN=20
style=3D"COLOR: #000000">10</SPAN><SPAN=20
style=3D"COLOR: #000000">)<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">false</SPAN><SPAN=20
style=3D"COLOR: #000000">; <BR>tempArray </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> DateString.split(Dilimeter);<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000"> (tempArray.length</SPAN><SPAN=20
style=3D"COLOR: #000000">!=3D</SPAN><SPAN style=3D"COLOR: =
#000000">3</SPAN><SPAN=20
style=3D"COLOR: #000000">)<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">false</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000"> (tempArray[</SPAN><SPAN=20
style=3D"COLOR: #000000">0</SPAN><SPAN=20
style=3D"COLOR: #000000">].length</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN style=3D"COLOR: =
#000000">4</SPAN><SPAN=20
style=3D"COLOR: #000000">)<BR>{<BR>tempy </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> tempArray[</SPAN><SPAN=20
style=3D"COLOR: #000000">0</SPAN><SPAN=20
style=3D"COLOR: #000000">];<BR>tempd </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> tempArray[</SPAN><SPAN=20
style=3D"COLOR: #000000">2</SPAN><SPAN=20
style=3D"COLOR: #000000">];<BR>}<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">else</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR>{<BR>tempy </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> tempArray[</SPAN><SPAN=20
style=3D"COLOR: #000000">2</SPAN><SPAN=20
style=3D"COLOR: #000000">];<BR>tempd </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> tempArray[</SPAN><SPAN=20
style=3D"COLOR: #000000">1</SPAN><SPAN=20
style=3D"COLOR: #000000">];<BR>}<BR>tempm </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> tempArray[</SPAN><SPAN=20
style=3D"COLOR: #000000">1</SPAN><SPAN=20
style=3D"COLOR: =
#000000">];<BR>var tDateString </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> tempy </SPAN><SPAN=20
style=3D"COLOR: #000000">+</SPAN><SPAN=20
style=3D"COLOR: #000000"> =E2=80=B2</SPAN><SPAN=20
style=3D"COLOR: #000000">/</SPAN><SPAN style=3D"COLOR: =
#000000">=E2=80=B2</SPAN><SPAN=20
style=3D"COLOR: #000000">+</SPAN><SPAN=20
style=3D"COLOR: #000000">tempm </SPAN><SPAN=20
style=3D"COLOR: #000000">+</SPAN><SPAN=20
style=3D"COLOR: #000000"> =E2=80=B2</SPAN><SPAN=20
style=3D"COLOR: #000000">/</SPAN><SPAN style=3D"COLOR: =
#000000">=E2=80=B2</SPAN><SPAN=20
style=3D"COLOR: #000000">+</SPAN><SPAN=20
style=3D"COLOR: #000000">tempd</SPAN><SPAN=20
style=3D"COLOR: #000000">+</SPAN><SPAN=20
style=3D"COLOR: #000000">=E2=80=B2 </SPAN><SPAN=20
style=3D"COLOR: #000000">8</SPAN><SPAN style=3D"COLOR: =
#000000">:</SPAN><SPAN=20
style=3D"COLOR: #000000">0</SPAN><SPAN style=3D"COLOR: =
#000000">:</SPAN><SPAN=20
style=3D"COLOR: #000000">0</SPAN><SPAN style=3D"COLOR: =
#000000">=E2=80=B2;</SPAN><SPAN=20
style=3D"COLOR: #008000">//</SPAN><SPAN=20
style=3D"COLOR: =
#008000">=E5=8A=A0=E5=85=AB=E5=B0=8F=E6=97=B6=E6=98=AF=E5=9B=A0=E4=B8=BA=E6=
=88=91=E4=BB=AC=E5=A4=84=E4=BA=8E=E4=B8=9C=E5=85=AB=E5=8C=BA</SPAN><SPAN =
style=3D"COLOR: #008000"><BR></SPAN><SPAN=20
style=3D"COLOR: #000000">var tempDate </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">new</SPAN><SPAN=20
style=3D"COLOR: #000000"> Date(tDateString);<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000"> (isNaN(tempDate))<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">false</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> (((tempDate.getUTCFullYear()).toString()</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN=20
style=3D"COLOR: #000000">tempy) </SPAN><SPAN=20
style=3D"COLOR: #000000">&&</SPAN><SPAN=20
style=3D"COLOR: #000000"> (tempDate.getMonth()</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN=20
style=3D"COLOR: #000000">parseInt(tempm)</SPAN><SPAN=20
style=3D"COLOR: #000000">-</SPAN><SPAN style=3D"COLOR: =
#000000">1</SPAN><SPAN=20
style=3D"COLOR: #000000">) </SPAN><SPAN=20
style=3D"COLOR: #000000">&&</SPAN><SPAN=20
style=3D"COLOR: #000000"> (tempDate.getDate()</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN=20
style=3D"COLOR: #000000">parseInt(tempd)))<BR>{<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -