1。判断是否是数字
function isNumber(pObj,errMsg){
var obj = eval(pObj);
strRef = "1234567890";
if(!isEmpty(pObj,errMsg))return false;
for (i=0;i<obj.value.length;i++) {
tempChar= obj.value.substring(i,i+1);
if (strRef.indexOf(tempChar,0)==-1) {
if (errMsg == null || errMsg =="")
alert("数据不符合要求,请检查");
else
alert(errMsg);
if(obj.type=="text")
obj.focus();
return false;
}
}
return true;
}
function isNumber(pObj,errMsg){
var obj = eval(pObj);
strRef = "1234567890";
if(!isEmpty(pObj,errMsg))return false;
for (i=0;i<obj.value.length;i++) {
tempChar= obj.value.substring(i,i+1);
if (strRef.indexOf(tempChar,0)==-1) {
if (errMsg == null || errMsg =="")
alert("数据不符合要求,请检查");
else
alert(errMsg);
if(obj.type=="text")
obj.focus();
return false;
}
}
return true;
}
2。判断是否是数字,数字可以为负数
function
isNegative(pObj,errMsg){
var
obj
=
eval(pObj);
strRef
=
"
1234567890-
"
;
if
(
!
isEmpty(pObj,errMsg))
return
false
;
for
(i
=
0
;i
<
obj.value.length;i
++
) {
tempChar
=
obj.value.substring(i,i
+
1
);
if
(strRef.indexOf(tempChar,
0
)
==-
1
) {
if
(errMsg
==
null
||
errMsg
==
""
)
alert(
"
数据不符合要求,请检查
"
);
else
alert(errMsg);
if
(obj.type
==
"
text
"
)
obj.focus();
return
false
;
}
else
{
if
(i
>
0
){
if
(obj.value.substring(i,i
+
1
)
==
"
-
"
){
if
(errMsg
==
null
||
errMsg
==
""
)
alert(
"
数据不符合要求,请检查
"
);
else
alert(errMsg);
if
(obj.type
==
"
text
"
)
obj.focus();
return
false
;
}
}
}
}
return
true
;
}
function
isNegative(pObj,errMsg){
var obj = eval(pObj);
strRef = " 1234567890- " ;
if ( ! isEmpty(pObj,errMsg)) return false ;
for (i = 0 ;i < obj.value.length;i ++ ) {
tempChar = obj.value.substring(i,i + 1 );
if (strRef.indexOf(tempChar, 0 ) ==- 1 ) {
if (errMsg == null || errMsg == "" )
alert( " 数据不符合要求,请检查 " );
else
alert(errMsg);
if (obj.type == " text " )
obj.focus();
return false ;
} else {
if (i > 0 ){
if (obj.value.substring(i,i + 1 ) == " - " ){
if (errMsg == null || errMsg == "" )
alert( " 数据不符合要求,请检查 " );
else
alert(errMsg);
if (obj.type == " text " )
obj.focus();
return false ;
}
}
}
}
return true ;
}
var obj = eval(pObj);
strRef = " 1234567890- " ;
if ( ! isEmpty(pObj,errMsg)) return false ;
for (i = 0 ;i < obj.value.length;i ++ ) {
tempChar = obj.value.substring(i,i + 1 );
if (strRef.indexOf(tempChar, 0 ) ==- 1 ) {
if (errMsg == null || errMsg == "" )
alert( " 数据不符合要求,请检查 " );
else
alert(errMsg);
if (obj.type == " text " )
obj.focus();
return false ;
} else {
if (i > 0 ){
if (obj.value.substring(i,i + 1 ) == " - " ){
if (errMsg == null || errMsg == "" )
alert( " 数据不符合要求,请检查 " );
else
alert(errMsg);
if (obj.type == " text " )
obj.focus();
return false ;
}
}
}
}
return true ;
}
3。/*IsInt(string,string,int or string):(测试字符串,+ or - or empty,empty or 0)
功能:判断是否为整数、正整数、负整数、正整数+0、负整数+0*/
function IsInt(objStr,sign,zero)
{
var reg;
var bolzero;
if(Trim(objStr)=="")
{
return false;
}
else
{
objStr=objStr.toString();
}
if((sign==null)||(Trim(sign)==""))
{
sign="+-";
}
if((zero==null)||(Trim(zero)==""))
{
bolzero=false;
}
else
{
zero=zero.toString();
if(zero=="0")
{
bolzero=true;
}
else
{
alert("检查是否包含0参数,只可为(空、0)");
}
}
switch(sign)
{
case "+-":
//整数
reg=/(^-?|^/+?)/d+$/;
break;
case "+":
if(!bolzero)
{
//正整数
reg=/^/+?[0-9]*[1-9][0-9]*$/;
}
else
{
//正整数+0
//reg=/^/+?/d+$/;
reg=/^/+?[0-9]*[0-9][0-9]*$/;
}
break;
case "-":
if(!bolzero)
{
//负整数
reg=/^-[0-9]*[1-9][0-9]*$/;
}
else
{
//负整数+0
//reg=/^-/d+$/;
reg=/^-[0-9]*[0-9][0-9]*$/;
}
break;
default:
alert("检查符号参数,只可为(空、+、-)");
return false;
break;
}
var r=objStr.match(reg);
if(r==null)
{
return false;
}
else
{
return true;
}
}
function IsInt(objStr,sign,zero)
{
var reg;
var bolzero;
if(Trim(objStr)=="")
{
return false;
}
else
{
objStr=objStr.toString();
}
if((sign==null)||(Trim(sign)==""))
{
sign="+-";
}
if((zero==null)||(Trim(zero)==""))
{
bolzero=false;
}
else
{
zero=zero.toString();
if(zero=="0")
{
bolzero=true;
}
else
{
alert("检查是否包含0参数,只可为(空、0)");
}
}
switch(sign)
{
case "+-":
//整数
reg=/(^-?|^/+?)/d+$/;
break;
case "+":
if(!bolzero)
{
//正整数
reg=/^/+?[0-9]*[1-9][0-9]*$/;
}
else
{
//正整数+0
//reg=/^/+?/d+$/;
reg=/^/+?[0-9]*[0-9][0-9]*$/;
}
break;
case "-":
if(!bolzero)
{
//负整数
reg=/^-[0-9]*[1-9][0-9]*$/;
}
else
{
//负整数+0
//reg=/^-/d+$/;
reg=/^-[0-9]*[0-9][0-9]*$/;
}
break;
default:
alert("检查符号参数,只可为(空、+、-)");
return false;
break;
}
var r=objStr.match(reg);
if(r==null)
{
return false;
}
else
{
return true;
}
}
4。/*IsFloat(string,string,int or string):(测试字符串,+ or - or empty,empty or 0)
功能:判断是否为浮点数、正浮点数、负浮点数、正浮点数+0、负浮点数+0*/
function
IsFloat(objStr,sign,zero)
{
var
reg;
var
bolzero;
if
(Trim(objStr)
==
""
)
{
return
false
;
}
else
{
objStr
=
objStr.toString();
}
if
((sign
==
null
)
||
(Trim(sign)
==
""
))
{
sign
=
"
+-
"
;
}
if
((zero
==
null
)
||
(Trim(zero)
==
""
))
{
bolzero
=
false
;
}
else
{
zero
=
zero.toString();
if
(zero
==
"
0
"
)
{
bolzero
=
true
;
}
else
{
alert(
"
检查是否包含0参数,只可为(空、0)
"
);
}
}
switch
(sign)
{
case
"
+-
"
:
//
浮点数
reg
=/^
((
-?|
/
+?
)/d
+
)(/./d
+
)
?
$
/
;
break
;
case
"
+
"
:
if
(
!
bolzero)
{
//
正浮点数
reg
=/^
/
+?
(([
0
-
9
]
+
/.[
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
/.[
0
-
9
]
+
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
))$
/
;
}
else
{
//
正浮点数+0
reg
=/^
/
+?
/d
+
(/./d
+
)
?
$
/
;
}
break
;
case
"
-
"
:
if
(
!
bolzero)
{
//
负浮点数
reg
=/^-
(([
0
-
9
]
+
/.[
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
/.[
0
-
9
]
+
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
))$
/
;
}
else
{
//
负浮点数+0
reg
=/^
((
-
/d
+
(/./d
+
)
?
)
|
(
0
+
(/.
0
+
)
?
))$
/
;
}
break
;
default
:
alert(
"
检查符号参数,只可为(空、+、-)
"
);
return
false
;
break
;
}
var
r
=
objStr.match(reg);
if
(r
==
null
)
{
return
false
;
}
else
{
return
true
;
}
}
function
IsFloat(objStr,sign,zero)
{
var reg;
var bolzero;
if (Trim(objStr) == "" )
{
return false ;
}
else
{
objStr = objStr.toString();
}
if ((sign == null ) || (Trim(sign) == "" ))
{
sign = " +- " ;
}
if ((zero == null ) || (Trim(zero) == "" ))
{
bolzero = false ;
}
else
{
zero = zero.toString();
if (zero == " 0 " )
{
bolzero = true ;
}
else
{
alert( " 检查是否包含0参数,只可为(空、0) " );
}
}
switch (sign)
{
case " +- " :
// 浮点数
reg =/^ (( -?| / +? )/d + )(/./d + ) ? $ / ;
break ;
case " + " :
if ( ! bolzero)
{
// 正浮点数
reg =/^ / +? (([ 0 - 9 ] + /.[ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * /.[ 0 - 9 ] + ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ))$ / ;
}
else
{
// 正浮点数+0
reg =/^ / +? /d + (/./d + ) ? $ / ;
}
break ;
case " - " :
if ( ! bolzero)
{
// 负浮点数
reg =/^- (([ 0 - 9 ] + /.[ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * /.[ 0 - 9 ] + ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ))$ / ;
}
else
{
// 负浮点数+0
reg =/^ (( - /d + (/./d + ) ? ) | ( 0 + (/. 0 + ) ? ))$ / ;
}
break ;
default :
alert( " 检查符号参数,只可为(空、+、-) " );
return false ;
break ;
}
var r = objStr.match(reg);
if (r == null )
{
return false ;
}
else
{
return true ;
}
}
{
var reg;
var bolzero;
if (Trim(objStr) == "" )
{
return false ;
}
else
{
objStr = objStr.toString();
}
if ((sign == null ) || (Trim(sign) == "" ))
{
sign = " +- " ;
}
if ((zero == null ) || (Trim(zero) == "" ))
{
bolzero = false ;
}
else
{
zero = zero.toString();
if (zero == " 0 " )
{
bolzero = true ;
}
else
{
alert( " 检查是否包含0参数,只可为(空、0) " );
}
}
switch (sign)
{
case " +- " :
// 浮点数
reg =/^ (( -?| / +? )/d + )(/./d + ) ? $ / ;
break ;
case " + " :
if ( ! bolzero)
{
// 正浮点数
reg =/^ / +? (([ 0 - 9 ] + /.[ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * /.[ 0 - 9 ] + ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ))$ / ;
}
else
{
// 正浮点数+0
reg =/^ / +? /d + (/./d + ) ? $ / ;
}
break ;
case " - " :
if ( ! bolzero)
{
// 负浮点数
reg =/^- (([ 0 - 9 ] + /.[ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * /.[ 0 - 9 ] + ) | ([ 0 - 9 ] * [ 1 - 9 ][ 0 - 9 ] * ))$ / ;
}
else
{
// 负浮点数+0
reg =/^ (( - /d + (/./d + ) ? ) | ( 0 + (/. 0 + ) ? ))$ / ;
}
break ;
default :
alert( " 检查符号参数,只可为(空、+、-) " );
return false ;
break ;
}
var r = objStr.match(reg);
if (r == null )
{
return false ;
}
else
{
return true ;
}
}
5。验证是否为空
function IsEmpty(obj)
{
obj=document.getElementsByName(obj).item(0);
if(Trim(obj.value)=="")
{
alert("字段不能为空。");
if(obj.disabled==false && obj.readOnly==false)
{
obj.focus();
}
}
}
function IsEmpty(obj)
{
obj=document.getElementsByName(obj).item(0);
if(Trim(obj.value)=="")
{
alert("字段不能为空。");
if(obj.disabled==false && obj.readOnly==false)
{
obj.focus();
}
}
}
6。检验用户输入的表名不能为空只能包含数字字母下划线并且只能以下划线和字母开头
function
checkdata(){
//
校验表名只能输入字母和数字及下划线
var
str
=
document.thisform.usetname.value;
var
strSource
=
"
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_
"
;
var
strNum
=
"
0123456789
"
;
var
ch;
var
i;
var
j;
var
temp;
var
nu
=
str.charAt(
0
);
var
temp1
=
strNum.indexOf(nu);
//
表头不能为数字
if
(temp1
!=-
1
)
{
alert(
"
您新增表名首字母不能为数字!
"
)
document.thisform.usetname1.focus();
document.thisform.usetname1.select();
return
false
;
}
for
(i
=
0
;i
<=
(str.length
-
1
);i
++
)
{
ch
=
str.charAt(i);
temp
=
strSource.indexOf(ch);
if
(temp
==-
1
)
{
alert(
"
您新增表名包含非法字符 ,只允许输入字母和数字!
"
)
document.thisform.usetname.focus();
document.thisform.usetname.select();
return
false
;
}
}
if
(isEmpty(document.thisform.usetname.value))
{
alert(
"
未输入自定义表名
"
);
document.thisform.usetname.focus();
return
;
}
}
7.JS计算的四舍五入笨方法~
<script language="javascript">
document.write("value="+sle(2300.365));
function sle(aa){
var va=""+aa;
var newvalue="";
var newfloat=0;
var newfloat2=0;
var len=va.lastIndexOf(".");
if(len<0){
newvalue=""+aa;
}
else if(len==(va.length-2)){
newvalue=""+aa+"0";
}
else if(len==(va.length-3)){
newvalue=""+aa;
}
else{
newfloat=Math.round(eval(aa*100),2);
newfloat2=newfloat/100;
newvalue=""+newfloat2;
}
return newvalue;
}
</script>
8.手机号码检查
<
script
>
function
aa(obj)
{
if
(obj.length
!=
11
) alert(
"
not
"
);
else
if
(obj.substring(
0
,
2
)
!=
"
13
"
) alert(
"
not
"
);
else
if
(isNaN(obj)) alert(
"
not
"
);
else
alert(
"
right
"
);
}
</
script
>
<
form
name
=codeN>
<input type
=text
name
=No
><
input
type
=button
value
='click'
onclick
=aa(document.codeN.No.value)>
</form
>
function
checkdata(){
// 校验表名只能输入字母和数字及下划线
var str = document.thisform.usetname.value;
var strSource = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ " ;
var strNum = " 0123456789 " ;
var ch;
var i;
var j;
var temp;
var nu = str.charAt( 0 );
var temp1 = strNum.indexOf(nu);
// 表头不能为数字
if (temp1 !=- 1 )
{
alert( " 您新增表名首字母不能为数字! " )
document.thisform.usetname1.focus();
document.thisform.usetname1.select();
return false ;
}
for (i = 0 ;i <= (str.length - 1 );i ++ )
{
ch = str.charAt(i);
temp = strSource.indexOf(ch);
if (temp ==- 1 )
{
alert( " 您新增表名包含非法字符 ,只允许输入字母和数字! " )
document.thisform.usetname.focus();
document.thisform.usetname.select();
return false ;
}
}
if (isEmpty(document.thisform.usetname.value))
{
alert( " 未输入自定义表名 " );
document.thisform.usetname.focus();
return ;
}
}
// 校验表名只能输入字母和数字及下划线
var str = document.thisform.usetname.value;
var strSource = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ " ;
var strNum = " 0123456789 " ;
var ch;
var i;
var j;
var temp;
var nu = str.charAt( 0 );
var temp1 = strNum.indexOf(nu);
// 表头不能为数字
if (temp1 !=- 1 )
{
alert( " 您新增表名首字母不能为数字! " )
document.thisform.usetname1.focus();
document.thisform.usetname1.select();
return false ;
}
for (i = 0 ;i <= (str.length - 1 );i ++ )
{
ch = str.charAt(i);
temp = strSource.indexOf(ch);
if (temp ==- 1 )
{
alert( " 您新增表名包含非法字符 ,只允许输入字母和数字! " )
document.thisform.usetname.focus();
document.thisform.usetname.select();
return false ;
}
}
if (isEmpty(document.thisform.usetname.value))
{
alert( " 未输入自定义表名 " );
document.thisform.usetname.focus();
return ;
}
}
<script language="javascript">
document.write("value="+sle(2300.365));
function sle(aa){
var va=""+aa;
var newvalue="";
var newfloat=0;
var newfloat2=0;
var len=va.lastIndexOf(".");
if(len<0){
newvalue=""+aa;
}
else if(len==(va.length-2)){
newvalue=""+aa+"0";
}
else if(len==(va.length-3)){
newvalue=""+aa;
}
else{
newfloat=Math.round(eval(aa*100),2);
newfloat2=newfloat/100;
newvalue=""+newfloat2;
}
return newvalue;
}
</script>
<
script
>
function aa(obj)
{
if (obj.length != 11 ) alert( " not " );
else if (obj.substring( 0 , 2 ) != " 13 " ) alert( " not " );
else if (isNaN(obj)) alert( " not " );
else alert( " right " );
}
</ script >
function aa(obj)
{
if (obj.length != 11 ) alert( " not " );
else if (obj.substring( 0 , 2 ) != " 13 " ) alert( " not " );
else if (isNaN(obj)) alert( " not " );
else alert( " right " );
}
</ script >
<
form
name
=codeN>
<input type =text name =No >< input type =button value ='click' onclick =aa(document.codeN.No.value)>
</form >
<input type =text name =No >< input type =button value ='click' onclick =aa(document.codeN.No.value)>
</form >
9.身份证号是否有效验证(15位的有误)
<
script
>
var aCity = { 11 : " 北京 " , 12 : " 天津 " , 13 : " 河北 " , 14 : " 山西 " , 15 : " 内蒙古 " , 21 : " 辽宁 " , 22 : " 吉林 " , 23 : " 黑龙江 " , 31 : " 上海 " , 32 : " 江苏 " , 33 : " 浙江 " , 34 : " 安徽 " , 35 : " 福建 " , 36 : " 江西 " , 37 : " 山东 " , 41 : " 河南 " , 42 : " 湖北 " , 43 : " 湖南 " , 44 : " 广东 " , 45 : " 广西 " , 46 : " 海南 " , 50 : " 重庆 " , 51 : " 四川 " , 52 : " 贵州 " , 53 : " 云南 " , 54 : " 西藏 " , 61 : " 陕西 " , 62 : " 甘肃 " , 63 : " 青海 " , 64 : " 宁夏 " , 65 : " 新疆 " , 71 : " 台湾 " , 81 : " 香港 " , 82 : " 澳门 " , 91 : " 国外 " }
function cidInfo(sId){
var iSum = 0
var info = ""
if ( !/^ /d{ 17 }(/d | x)$ / i.test(sId)) return false ;
sId = sId.replace( / x$ / i, " a " );
if (aCity[parseInt(sId.substr( 0 , 2 ))] == null ) return " Error:非法地区 " ;
sBirthday = sId.substr( 6 , 4 ) + " - " + Number(sId.substr( 10 , 2 )) + " - " + Number(sId.substr( 12 , 2 ));
var d = new Date(sBirthday.replace( /-/ g, " / " ))
if (sBirthday != (d.getFullYear() + " - " + (d.getMonth() + 1 ) + " - " + d.getDate())) return " Error:非法生日 " ;
for ( var i = 17 ;i >= 0 ;i -- ) iSum += (Math.pow( 2 ,i) % 11 ) * parseInt(sId.charAt( 17 - i), 11 )
if (iSum % 11 != 1 ) return " Error:非法证号 " ;
return aCity[parseInt(sId.substr( 0 , 2 ))] + " , " + sBirthday + " , " + (sId.substr( 16 , 1 ) % 2 ? " 男 " : " 女 " )
}
</ script >
var aCity = { 11 : " 北京 " , 12 : " 天津 " , 13 : " 河北 " , 14 : " 山西 " , 15 : " 内蒙古 " , 21 : " 辽宁 " , 22 : " 吉林 " , 23 : " 黑龙江 " , 31 : " 上海 " , 32 : " 江苏 " , 33 : " 浙江 " , 34 : " 安徽 " , 35 : " 福建 " , 36 : " 江西 " , 37 : " 山东 " , 41 : " 河南 " , 42 : " 湖北 " , 43 : " 湖南 " , 44 : " 广东 " , 45 : " 广西 " , 46 : " 海南 " , 50 : " 重庆 " , 51 : " 四川 " , 52 : " 贵州 " , 53 : " 云南 " , 54 : " 西藏 " , 61 : " 陕西 " , 62 : " 甘肃 " , 63 : " 青海 " , 64 : " 宁夏 " , 65 : " 新疆 " , 71 : " 台湾 " , 81 : " 香港 " , 82 : " 澳门 " , 91 : " 国外 " }
function cidInfo(sId){
var iSum = 0
var info = ""
if ( !/^ /d{ 17 }(/d | x)$ / i.test(sId)) return false ;
sId = sId.replace( / x$ / i, " a " );
if (aCity[parseInt(sId.substr( 0 , 2 ))] == null ) return " Error:非法地区 " ;
sBirthday = sId.substr( 6 , 4 ) + " - " + Number(sId.substr( 10 , 2 )) + " - " + Number(sId.substr( 12 , 2 ));
var d = new Date(sBirthday.replace( /-/ g, " / " ))
if (sBirthday != (d.getFullYear() + " - " + (d.getMonth() + 1 ) + " - " + d.getDate())) return " Error:非法生日 " ;
for ( var i = 17 ;i >= 0 ;i -- ) iSum += (Math.pow( 2 ,i) % 11 ) * parseInt(sId.charAt( 17 - i), 11 )
if (iSum % 11 != 1 ) return " Error:非法证号 " ;
return aCity[parseInt(sId.substr( 0 , 2 ))] + " , " + sBirthday + " , " + (sId.substr( 16 , 1 ) % 2 ? " 男 " : " 女 " )
}
</ script >
<
html
>
< head >
< title > 身份证号是否有效验证 </ title >
</ head >
< body >
请输入身份证号码: < input type ="text" size ="20" id ="asidinput" />< input type ="button" value ="验证" onclick ="alert(cidInfo(asidinput.value))" />
</ body >
</ html >
< head >
< title > 身份证号是否有效验证 </ title >
</ head >
< body >
请输入身份证号码: < input type ="text" size ="20" id ="asidinput" />< input type ="button" value ="验证" onclick ="alert(cidInfo(asidinput.value))" />
</ body >
</ html >
10.检查一段字符串是否全由数字组成
<
script language
=
"
Javascript
"
>
function checkNum(str){ return str.match( / /D / ) == null }
alert(checkNum( " 1232142141 " ))
alert(checkNum( " 123214214a1 " ))
</script>
function checkNum(str){ return str.match( / /D / ) == null }
alert(checkNum( " 1232142141 " ))
alert(checkNum( " 123214214a1 " ))
</script>