常用验证汇总表
- 保证添加内容可以,复制后直接运行
- 尽量详细的添加一些实现原理说明
var checkable = function(element) {
return /radio|checkbox/i.test(element.type);
};
function required(elem, value){
if (elem.nodeName.toLowerCase() === 'select') {
var v = elem.value;
return v && v != 0 && v != '';
}
if (checkable(elem)) {
return getLength(value, elem) > 0;
}
return value.length > 0;
}
/^[1-9][0-9]{5}$/.test(value)
TODO: 手机号如果增加新的号段,需要手动更新
/^(13|14|15|16|17|18|19)\d{9}$/.test(value)
/^([a-zA-Z0-9_\.\-+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value)
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(value)
/^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/.test(value)
/<(.*)>.*<\/>|<(.*) \/>/.test(value)
身份证15位编码规则:dddddd yymmdd xx p
dddddd:地区码
yymmdd: 出生年月日
xx: 顺序类编码,无法确定
p: 性别,奇数为男,偶数为女
身份证18位编码规则:dddddd yyyymmdd xxx y
dddddd:地区码
yyyymmdd: 出生年月日
xxx:顺序类编码,无法确定,奇数为男,偶数为女
y: 校验码,该位数值可通过前17位计算获得
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 isIdCard(arrIdCard){
arrIdCard = arrIdCard.length == 15 ? idCard15To18(arrIdCard) : arrIdCard;
var tag = false;
var sigma = 0;
var a = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
var w = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
for (var i = 0; i < 17; i++) {
var ai = parseInt(arrIdCard.substring(i, i + 1));
var wi = a[i];
sigma += ai * wi;
}
var number = sigma % 11;
var check_number = w[number];
if (arrIdCard.substring(17) != check_number) {
tag = false;
} else {
tag = true;
}
return tag;
}
function idCard15To18(id){
var W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
var A = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
var i, j, s = 0;
var newid;
newid = id;
newid = newid.substring(0, 6) + "19" + newid.substring(6, id.length);
for(i = 0; i < newid.length; i++ ){
j = parseInt(newid.substring(i,i+1))*W[i];
s = s + j;
}
s = s % 11;
newid = newid + A[s];
return newid;
}
//通过身份证号判断是男性还是女性
//男性返回M,女性返回F
function isFOrM(idCard){
idCard = idCard.length == 15 ? idCard15To18(idCard) : idCard;
if(idCard.substring(14,17)%2 == 0){
return 'F';
}else{
return 'M';
}
}
/**
* 获取年龄
* @param id
* @returns {number} 返回当前周岁(已过生日默认+1岁)
*/
function getAge(id){
id = idCard15To18(id);
var day = new Date();
nowYear = day.getFullYear(),
nowMonth = day.getMonth() + 1,
nowDay = day.getDate(),
idDay = id.substring(6, 14),
idcardYear = idDay.substring(0, 4),
idcardMonth = idDay.substring(4, 6),
idcardDay = idDay.substring(6)
var age = nowYear - idcardYear,
M = nowMonth - parseInt(idcardMonth),
D = nowDay - parseInt(idcardDay);
return (M>0 || (M == 0 && D >= 0)) ? age+1 : age;
}
function isCreditCard(number){
function reverseString(source){
var s=source;
var ss="";
for(var i=s.length-1;i>=0;i--){
ss=ss+s.charAt(i);
}
return ss;
}
try{
if(number.length==0 || number.length<12 || number.length>19){
return false;
}
var exp = /[34569]/,
objExp = new RegExp(exp);
if(objExp.test(number.charAt(0)==false)) return false;
var n = reverseString(number),
s = 0,
d = 0;
for(var i=0;i<n.length;i++){
//说明是基数,因为从0位开始
if (i%2==0){
s = s + n.charAt(i) * 1
} else {
var t = n.charAt(i) * 2;
if(t > 9){
d = d + (t/10|0) + t % 10;
}else{
d = d + t;
}
}
}
var sum = s + d;
if(sum%10 == 0){
return true;
} else {
return false
}
}catch (e){
return false;
}
}
function byteRangeLength(value){
var length = value.length;
for(var i = 0; i < value.length; i++){
if(value.charCodeAt(i) > 127){
length++;
}
}
if(param.length === 1) {param[1] = param[0]};
return length >= param[0] && length <= param[1];
}
/^[\u4e00-\u9fa5],{0,}$/.test(value)
/^(\d{3,4}-)?\d{7,8}$/.test(value)
/\.(jpe?g|gif|bmp|png)/i.test(value)
/^\d{5,10}$/.test(value)
!isNaN(value)
/^\d+$/.test(value)
/^(-?\d+)(\.\d+)?$/.test(value)
/^((-?\d)\.\d+)?$/.test(value)
/^(?:0[1-9]|1[0-2])\/(?:0[1-9]|[12][0-9]|3[01])\/(?:\d{4})/.test(value)
PS: 可以到这里查看一些实用的基础方法