validate.js
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* 常用的验证集合
* useage:if(validate.isemail(email)){...}else{....}
* last update 2015-07-29
*/
String.prototype.trim = function(){
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim = function(){
return this.replace(/(^\s*)/g, "");
}
String.prototype.rtrim = function(){
return this.replace(/(\s*$)/g, "");
}
var validate = {
//是否电话号码
isphone: function (value) {
var patrn = /^((\+?86)|(\(\+86\)))?\d{3,4}-\d{7,8}(-\d{3,4})?$/
if (!patrn.exec(value)) return false
return true
},
//是否手机号码
iscellphone: function (value) {
var validateReg = /^((\+?86)|(\(\+86\)))?1\d{10}$/;
return validateReg.test(value);
},
//是否邮箱
isemail: function(value){
var emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
return emailRegex.test(value);
},
//是否为空
isempty: function(value){
if(value.length == 0) return true
return false;
},
//是否为18位身份证号码
id18: function (id) {
if(! /\d{17}[\dxX]/.test(id)) {
return false;
}
var modcmpl = function(m, i, n) { return (i + n - m % i) % i; },
f = function(v, i) { return v * (Math.pow(2, i-1) % 11); },
s = 0;
for(var i=0; i<17; i++) {
s += f(+id.charAt(i), 18-i);
}
var c0 = id.charAt(17),
c1 = modcmpl(s, 11, 1);
return c0-c1===0 || (c0.toLowerCase()==='x' && c1===10);
},
//是否为15位身份证号码,2013年1月1日起停止使用
id15:function (id) {
var pattern = /[1-9]\d{5}(\d{2})(\d{2})(\d{2})\d{3}/;
matches, y, m, d, date;
matches = id.match(pattern);
y = +('19' + matches[1]);
m = +matches[2];
d = +matches[3];
date = new Date(y, m-1, d);
return (date.getFullYear()===y && date.getMonth()===m-1 && date.getDate()===d);
},
//是否为IP地址
isip:function(ip){
var ipRegex = /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/i;
return ipRegex.test(ip);
},
//是否为URL地址
isurl:function(url){
var urlRegex = /^((http|https):\/\/(\w+:{0,1}\w*@)?(\S+)|)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
return urlRegex.test(url);
},
haschinese:function(value){
var patrn=/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;
if(!patrn.exec(value)){
return false;
}else{
return true;
}
}
}