欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js一组验证函数

程序员文章站 2023-11-29 16:49:22
复制代码 代码如下://字符处理; //去左右空格; function trim(s){ return rtrim(ltrim(s)); } //去左空格; functio...
复制代码 代码如下:

//字符处理;
//去左右空格;
function trim(s){
return rtrim(ltrim(s));
}
//去左空格;
function ltrim(s){
return s.replace( /^\s*/, "");
}
//去右空格;
function rtrim(s){
return s.replace( /\s*$/, "");
}
//验证信息;
//空字符值;
function isempty(s){
s = trim(s);
return s.length == 0;
}
//email;
function isemail(s){
s = trim(s);
var p = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i;
return p.test(s);
}
//数字;
function isnumber(s){
return !isnan(s);
}
//颜色值;
function iscolor(s){
s = trim(s);
if (s.length !=7) return false;
return s.search(/\#[a-fa-f0-9]{6}/) != -1;
}
//手机号码;
function ismobile(s){
s = trim(s);
var p = /13\d{9}/;
return p.test(s);
}
//身份证;
function iscard(s){
s = trim(s);
var p = /^\d{15}(\d{2}[xx0-9])?$/;
return p.test(s);
}
//url;
function isurl(s){
s = trim(s).tolowercase();
var p = /^http:\/\/[a-za-z0-9]+\.[a-za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
return p.test(s);
}
//phone;
function isphone(s){
s = trim(s);
var p = /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;
return p.test(s);
}
//zip;
function iszip(s){
s = trim(s);
var p = /^[1-9]\d{5}$/;
return p.test(s);
}
//double;
function isdouble(s){
s = trim(s);
var p = /^[-\+]?\d+(\.\d+)?$/;
return p.test(s);
}
//integer;
function isinteger(s){
s = trim(s);
var p = /^[-\+]?\d+$/;
return p.test(s);
}
//english;
function isenglish(s){
s = trim(s);
var p = /^[a-za-z]+$/;
return p.test(s);
}
//中文;
function ischinese(s){
s = trim(s);
var p = /^[\u0391-\uffe5]+$/;
return p.test(s);
}
//双字节
function isdoublechar(s){
var p = /^[^\x00-\xff]+$/;
return p.test(s);
}
//含有中文字符
function haschinesechar(s){
var p = /[^\x00-\xff]/;
return p.test(s);
}
function hasaccountchar(s){
var p = /^[a-za-z0-9][a-za-z0-9_-]{0,15}$/;
return p.test(s);
}
function limitlen(s,min,max){
s=trim(s);
if(s=="") return false;
if((s.length<min)||(s.length>max))
return false;
else
return true;
}