export default function validateService() {
this.notNull = function(value) {
if (!value || ('' + value).trim().length <= 0) {
return false;
}
return true;
}
//校验手机号码
this.number = function(phoneNumber) {
if (!phoneNumber) {
return false;
} else {
phoneNumber = phoneNumber.replace(/(^\s*)|(\s*$)/g, "");
if (!phoneNumber.match(/^0?1[3|4|5|7|8][0-9]\d{8}$/)) {
return false;
}
}
return true;
};
//校验电子邮箱
this.email = function(email) {
if (!email) {
return false;
} else {
email = email.replace(/(^\s*)|(\s*$)/g, "");
if (!email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)) {
return false;
}
}
return true;
};
/**校验ip */
this.ip = function(value) {
var re = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
return re.test(value);
};
/**校验域名 */
this.domain = function(value) {
var reg = /^[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
// var reg=/^([a-zA-Z\d][a-zA-Z\d-_]+\.)+[a-zA-Z\d-_][^ ]*$/;
return reg.test(value);
};
/*****校验URL*****/
this.url = function(value){
var reg = /^((ht|f)tps?):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?/;
return reg.test(value);
}
this.port = function(value) {
const port = parseInt(value);
if (port > 0 && port < 65535) {
return true;
}
return false;
};
this.pureNum = function(n) {
if (!n||n==' ') {
return false;
} else {
n = n.replace(/(^\s*)|(\s*$)/g, "");
if (!n.match(/^[0-9]*$/)) {
return false;
}
}
return true;
};
//校验邮编
this.checkPost = function(code) {
if (!code) {
return false;
} else {
if (!code.match(/^[1-9][0-9]{5}$/)) {
return false;
}
return true;
}
};
}
js 中邮箱,手机号码,HTTPS,邮编,ip等常见的正则匹配
最新推荐文章于 2024-06-04 16:40:42 发布