常用的js驗證數字,電話號碼,傳真,郵箱,手機
系統
2808 0
1、數字
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
function
testisNum(object)
{
var
s
=
document.getElementById(object.id).value;
if
(s
!=
""
)
{
if
(isNaN(s))
{
alert(
"
請輸入數字
"
);
object.value
=
""
;
object.focus();
}
}
}
2、電話號碼,傳真
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
//
校驗普通電話、傳真號碼:可以“+”開頭,除數字外,可含有“-”
function
isTel(object)
{
//
國家代碼(2到3位)-區號(2到3位)-電話號碼(7到8位)-分機號(3位)"
var
s
=
document.getElementById(object.id).value;
var
pattern
=
/
^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$
/
;
//
varpattern=/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/;
if
(s
!=
""
)
{
if
(
!
pattern.exec(s))
{
alert(
'
請輸入正確的電話號碼:電話號碼格式為國家代碼(2到3位)-區號(2到3位)-電話號碼(7到8位)-分機號(3位)"
'
);
object.value
=
""
;
object.focus();
}
}
}
3、郵箱
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
function
Check(object)
{
var
s
=
document.getElementById(object.id).value;
var
pattern
=
/
^[a-zA-Z0-9_\-]{1,}@[a-zA-Z0-9_\-]{1,}\.[a-zA-Z0-9_\-.]{1,}$
/
;
if
(s
!=
""
)
{
if
(
!
pattern.exec(s))
{
alert(
'
請輸入正確的郵箱地址
'
);
object.value
=
""
;
object.focus();
}
}
}
4、手機號碼
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
//
校驗手機號碼:必須以數字開頭,除數字外,可含有“-”
function
isMobile(object)
{
var
s
=
document.getElementById(object.id).value;
var
reg0
=
/
^13\d{5,9}$
/
;
var
reg1
=
/
^153\d{4,8}$
/
;
var
reg2
=
/
^159\d{4,8}$
/
;
var
reg3
=
/
^0\d{10,11}$
/
;
var
my
=
false
;
if
(reg0.test(s))my
=
true
;
if
(reg1.test(s))my
=
true
;
if
(reg2.test(s))my
=
true
;
if
(reg3.test(s))my
=
true
;
if
(s
!=
""
)
{
if
(
!
my)
{
alert(
'
請輸入正確的手機號碼
'
);
object.value
=
""
;
object.focus();
}
}
}
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
//
校驗日期
function
isdate(object)
{
var
s
=
document.getElementById(object.id).value;
var
pattern
=
/
^((\d{2}(([02468][048])|([13579][26]))[\-\
/
\s]
?
((((
0
?
[
13578
])
|
(
1
[
02
]))[\
-
\
/
\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\
/
\s]
?
((
0
?
[
1
-
9
])
|
([
1
-
2
][
0
-
9
])
|
(
30
)))
|
(
0
?
2
[\
-
\
/
\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\
/
\s]
?
((((
0
?
[
13578
])
|
(
1
[
02
]))[\
-
\
/
\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\
/
\s]
?
((
0
?
[
1
-
9
])
|
([
1
-
2
][
0
-
9
])
|
(
30
)))
|
(
0
?
2
[\
-
\
/
\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$
/
;
if
(s
!=
""
)
{
if
(
!
pattern.exec(s))
{
alert(
'
請輸入正確的日期
'
);
object.value
=
""
;
object.focus();
}
}
}
5、郵編
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
//
校驗(國內)郵政編碼
function
isPostalCode(object)
{
var
s
=
document.getElementById(object.id).value;
var
pattern
=
/
^[0-9]{6}$
/
;
if
(s
!=
""
)
{
if
(
!
pattern.exec(s))
{
alert(
'
請輸入正確的郵政編碼
'
);
object.value
=
""
;
object.focus();
}
}
}
6、日期
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
//
校驗日期
function
isdate(object)
{
var
s
=
document.getElementById(object.id).value;
var
pattern
=
/
^((\d{2}(([02468][048])|([13579][26]))[\-\
/
\s]
?
((((
0
?
[
13578
])
|
(
1
[
02
]))[\
-
\
/
\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\
/
\s]
?
((
0
?
[
1
-
9
])
|
([
1
-
2
][
0
-
9
])
|
(
30
)))
|
(
0
?
2
[\
-
\
/
\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\
/
\s]
?
((((
0
?
[
13578
])
|
(
1
[
02
]))[\
-
\
/
\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\
/
\s]
?
((
0
?
[
1
-
9
])
|
([
1
-
2
][
0
-
9
])
|
(
30
)))
|
(
0
?
2
[\
-
\
/
\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$
/
;
if
(s
!=
""
)
{
if
(
!
pattern.exec(s))
{
alert(
'
請輸入正確的日期
'
);
object.value
=
""
;
object.focus();
}
}
}
常用的js驗證數字,電話號碼,傳真,郵箱,手機號碼,郵編,日期
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元