日期:2014-05-16  浏览次数:20398 次

Javascript 常用函数大全
http://www.cnitblog.com/flutist1225/articles/18780.html




一、验证类

1、数字类
1.1 整数
/*********
* signed int
*
*/
function IsInt(str)
{
if(/^(-|\+)?\d+$/.test(str))
{
  return true;
}
else
{
  alert("false");
  return false;
}
}
1.2 正整数 (包含0)
/*********
* unsigned int str >=0
*
*/
function IsUInt(str)
{
if(/^\d+$/.test(str))
{
alert("true");
  return true;
}
else
{
  alert("false");
  return false;
}
}
1.3字节
function isByte(x)
{
  return (!isNaN(x) && x >= 0 && x <= 255);
}
1.4无符号整数
function isUnsignedInt(x)
{
  return (!isNaN(x) && x > 0 && x <= 65535);
}

1.5 负整数
/*********
* int str <0
*
*/
function Is_Int(str)
{
if ( /^-\d+$/.test(str))
{
alert("true");
  return true;
}
else
{
  alert("false");
  return false;
}
}
1.6小于等于iMax 整数
/*********
* int str <=iMAX
*
*/
function IntLowMax(str,iMAX)
{
if ((/^\d+$/.test(str)) && (str <= iMAX))
{
alert("true");
  return true;
}
else
{
  alert("false");
  return false;
}
}
1.7 大于等于iMin整数
/*********
* int str >=iMin
*
*/
function IntHighMin(str,iMin)
{
if ((/^\d+$/.test(str)) && (str >= iMin))
{
alert("true");
  return true;
}
else
{
  alert("false");
  return false;
}
}
1.8 iMin-iMax 之间的整数
/***********************
* int iMin<=str <=iMax
*
************************/
function IntinMinMax(str,iMin,iMax)
{
if ((/^\d+$/.test(str)) && (iMin<=parseInt(str)) &&(parseInt(str)<=iMax))
{
  return true;
}
else
{
  alert("false!");
  return false;
}
}
1.9 是十六进制数
function isHexStr(sTest){
var result = true;
var m;
sTest = String(x).replace(/^\s+|\s+$/g,"");
m = sTest.match(/^([0-9a-fA-F]*)$/);

if(m == null)
{
alert("Is not Hex string!");
result = false;
}
return result;
}
2、字符类
2.1 a-Z或A-Z的字母组成
/***********************
*  just a string a-zA-Z
*
************************/
function IsChar(str)
{
if (/[^a-zA-Z]/g.test(str))
{
  alert("nor char!!");
  return false;
}
else
{
  return true;
}
}
2.2 字母和数字组成
/***********************
*  just a string a-zA-Z0-9
*
************************/
function IsDigitalChar(str)
{
if (/[^0-9a-zA-Z]/g.test(str))
{
  alert("nor char!!");
  return false;
}
else
{
  return true;
}
}

2.3 由字母和数字,下划线组成.且开头的只能是下划线和字母
/***********************
*  a string a-zA-Z0-9_
*
************************/
function IsDigitalChar_(str)
{
if (/^([a-zA-z_]{1})([\w]*)$/g.test(str))

  return true;
}
else
{
alert("not char!!");
  return false;
}
}
2.4 字符串替换函数.Replace();

3、网络类
3.1 IP_address 点分十进制表示 (e.g: 172.17.8.1)
function isip(s){
var check=function(v){try{return (v<=255 && v>=0)}catch(x){return false}};
var re=s.split(".");

if( (re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re