javascript 常用验证函数

本文介绍了一系列自定义的JavaScript字符串扩展方法,包括去除空格、截取字符串、验证IP地址、手机号、电子邮件等实用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* 去掉左右空格
*/
String.prototype.trim=function()
{
return this.replace(/^\s+|\s+$/g,"");
}
/**
* 去掉左边空格
*/
String.prototype.ltrim=function()
{
return this.replace(/^\s+/,"");
}
/**
* 去掉右边空格
*/
String.prototype.rtrim=function()
{
return this.replace(/\s+$/,"");
}
/**
* 获得左边字符串
*/
String.prototype.left = function(len)
{
if(isNaN(len)||len==null)
{
len = this.length;
}
else
{
if(parseInt(len)<0||parseInt(len)>this.length)
{
len = this.length;
}
}
return this.substring(0,len);
}


/**
* 获得右边字符串
*/
String.prototype.right = function(len)
{
if(isNaN(len)||len==null)
{
len = this.length;
}
else
{
if(parseInt(len)<0||parseInt(len)>this.length)

{
len = this.length;
}

}

return this.substring(this.length-len,this.length);
}


/**
* 获得中间字符串
*/
String.prototype.mid = function(start,len)
{

return this.substring(start,len);
}

/**
* 返回字符串所在的位置
*/
String.prototype.instr = function(str)
{
if(str==null)
{
str = "";
}

return this.indexOf(str);
}

/**
* 验证是否是IP
*/
String.prototype.isIP = function()
{
var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;

if (reSpaceCheck.test(this))
{

this.match(reSpaceCheck);

if (RegExp.$1 <= 255 && RegExp.$1 >= 0

&& RegExp.$2 <= 255 && RegExp.$2 >= 0

&& RegExp.$3 <= 255 && RegExp.$3 >= 0

&& RegExp.$4 <= 255 && RegExp.$4 >= 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

/**
*验证是否是手机号
*/
String.prototype.isMobile = function()
{
return /^0{0,1}13[0-9]{9}$/.test(this);
}

/**
* 验证是否是E-Mail
*/
String.prototype.isEmail = function()
{
return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);
}

/**
*验证是否存在汉字
*/
String.prototype.existChinese = function()
{
return /^[\x00-\xff]*$/.test(this);

}

/**
*验证是否是正确的身份证号
*/
String.prototype.isIDCard = function()
{
var iSum=0;
var info="";
var sId = this;

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:"国外"};

if(!/^\d{17}(\d|x)$/i.test(sId))
{
return false;
}
sId=sId.replace(/x$/i,"a");
//非法地区
if(aCity[parseInt(sId.substring(0,2))]==null)
{
return false;
}

var sBirthday=sId.substring(6,4)+"-"+Number(sId.substring(10,2))+"-"+Number(sId.substring(12,2));

var d=new Date(sBirthday.replace(/-/g,"/"))

//非法生日
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
{
return false;
}
for(var i = 17;i>=0;i--)
{
iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11);
}

if(iSum%11!=1)
{
return false;
}

return true;
}

/**
* 验证是否是正确的电话号码
*/
String.prototype.isPhoneCall = function()
{
return /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this);
}

/**
* 验证是否是数字
*/
String.prototype.isNumeric = function(flag)
{
if(isNaN(this))
{

return false;
}

switch(flag)
{

case null: //数字
case "":
return true;
case "+": //正数
return /(^\+?|^\d?)\d*\.?\d+$/.test(this);
case "-": //负数
return /^-\d*\.?\d+$/.test(this);
case "i": //整数
return /(^-?|^\+?|\d)\d+$/.test(this);
case "+i": //正整数
return /(^\d+$)|(^\+?\d+$)/.test(this);
case "-i": //负整数
return /^[-]\d+$/.test(this);
case "f": //浮点数
return /(^-?|^\+?|^\d?)\d*\.\d+$/.test(this);
case "+f": //正浮点数
return /(^\+?|^\d?)\d*\.\d+$/.test(this);
case "-f": //负浮点数
return /^[-]\d*\.\d$/.test(this);
default: //缺省
return true;
}
}

/**
*是否是颜色(#FFFFFF形式)
*/
String.prototype.IsColor = function()
{
var temp = this;
if (temp=="") return true;
if (temp.length!=7) return false;

return (temp.search(/\#[a-fA-F0-9]{6}/) != -1);
}

/**
*对字符串进行Html编码
*/
String.prototype.toHtmlEncode = function()
{
var str = this;

str=str.replace(/&/g,"&");

str=str.replace(/</g,"<");

str=str.replace(/>/g,">");

str=str.replace(/\'/g,"&apos;");

str=str.replace(/\"/g,""");

str=str.replace(/\n/g,"<br>");

str=str.replace(/\ /g," ");

str=str.replace(/\t/g,"    ");

return str;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值