赞
踩
参考网址:https://juejin.im/post/5965943ff265da6c30653879
REG_Chinese_English_Digit: /^[\u4e00-\u9fa5a-zA-Z0-9 、 .\+\(\)()\/]+$/,
REG_Chinese_English_Digit_Message: "只能输入中文、英文、数字,和小数点",
REG_Chinese_English_Digit_lf: /^[\u4e00-\u9fa5a-zA-Z0-9 、 \+\(\)()\/]+$/,
REG_Chinese_English_Digit_lf_Message: "只能输入中文、英文、数字",
var reg = /.+?(省|市|自治区|自治州|县|区)/g // 省市区的正则
var regex = "(?<province>[^省]+省|.+自治区)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?<county>[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
REG_English_Digit: /^[a-zA-Z0-9]+$/,
REG_English_Digit_Message: "只能输入字母、数字",
REG_Price: /^(\d+(\.\d{1,2})?)$/,
REG_Price_Message: "请输入正确的价格格式",
REG_Digit: /^[0-9]+$/,
REG_Digit_Message: "只能输入数字",
REG_English: /^[a-zA-Z ]+$/,
REG_English_Message: "只能输入英文",
REG_Phone: /^0\d{2,3}-?\d{7,8}$/,
REG_Phone_Message: "请输入正确的电话号码!",
REG_tePhone: /^1\d{10}$/,
REG_tePhone_Message: "请输入正确的手机号码!",
REG_allPhone: /^(^(\d{3,4}-)?\d{7,8})$|(13[0-9]{9})$/,
REG_allPhone_Message: "请输入正确的手机或固话号码!",
REG_chinese: /^[\u4e00-\u9fa5]+$/,
REG_chinese_Message: "请输入正确的中文!",
REG_email: /^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT)$/,
REG_email_Message: "请输入正确的邮箱!",
REG_smallNumber: /^[0-9]+([.]{1}[0-9]+){0,1}$/,
REG_smallNumber_Message: "请输入数字和小数!",
validateZeroToHundred: function (rule, value, callback) {
let reg = /^[0-9]+$/;
if (value === '' || isNaN(value) || Math.floor(value) != value|| reg.test(value) != true || value < 0 || value > 100) {
callback(new Error('请输入0~100之间的正整数'));
}
callback();
},
validatePositiveInteger: function (rule, value, callback) {
let reg = /^[0-9]+$/;
if (value === '' || isNaN(value) || reg.test(value) != true || value <= 0) {
callback(new Error('请输入大于0的正整数'));
}
callback();
},
validateDoubleBelowHundred: function (rule, value, callback) {
if (value === '' || isNaN(value) || value < 0.1 || value > 99.9) {
callback(new Error('请输入0.1~99.9之间的系数'));
} else if (value.split(".")[1]) {
if (value.split(".")[1].length > 1) {
callback(new Error('只能输入一位小数!'));
}
}
callback();
},
validateUpZero: function (rule, value, callback) {
if (value <= 0) {
callback(new Error('不能小于0'));
}
callback();
},
validateTinyint: function (rule, value, callback) {
let reg = /^[0-9]+$/;
if (value === '' || isNaN(value) || reg.test(value) != true || (value <= 0 || value > 127)) {
callback(new Error('请输入1~127之间的整数'));
}
callback();
},
validatePrice: function (rule, value, callback) {
let reg = /^([-]{0,1})(\d+(\.\d{1,2})?)$/;
if (value != '' && value != null && !reg.test(value)) {
callback(new Error('请输入正确的价格格式'));
}
if (value < 0) {
callback(new Error('价格不能小于0'));
}
if (value > 999999999999.99) {
callback(new Error('价格不能大于999999999999.99'));
}
callback();
},
validateInt: function (rule, value, callback) {
let reg = /^[0-9]+$/;
if (value != '' && value != null && !reg.test(value)) {
callback(new Error('请输入整数值'));
}
if (value > 999999999) {
callback(new Error('不能大于999999999'));
}
callback();
},
validateIP: function (rule, value, callback) {
let reg = /^(\d+)\.(\d+)\.(\d+)\.(\d+)+$/;
if (value != '' && value != null && !reg.test(value)) {
callback(new Error('请输入正确的ip地址'));
}
callback();
}
validatePassWord: function (rule, value, callback) {
let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/;
if (value != '' && value != null && !reg.test(value)) {
callback(new Error('请输入正确格式的密码!'));
}
callback();
}
REG_qqNumber: /^(^(\d{3,4}-)?\d{7,8})$|(13[0-9]{9})$/,
REG_qqNumber_Message: "请输入正确的手机或固话号码!",
E-mail 验证
下面的函数检查输入的数据是否符合电子邮件地址的基本语法。
意思就是说,输入的数据必须包含 @ 符号和点号(.)。同时,@ 不可以是邮件地址的首字符,并且 @ 之后需有至少一个点号:
function validateForm(){
var x=document.forms["myForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
alert("不是一个有效的 e-mail 地址");
return false;
}
}
新增 util/RegExpression.js 文件
export default {
//中文、英文,数字和小数点
REG_Chinese_English_Digit: /^[\u4e00-\u9fa5a-zA-Z0-9 、 .\+\(\)()\/]+$/,
REG_Chinese_English_Digit_Message: "只能输入中文、英文、数字,和小数点",}
validateIP: function (rule, value, callback) {
let reg = /^(\d+)\.(\d+)\.(\d+)\.(\d+)+$/;
if (value != '' && value != null && !reg.test(value)) {
callback(new Error('请输入正确的ip地址'));
}
callback();
}
startTime:[
{ max: 20, message: '长度不能超过20 个字符', trigger: 'change' },
{ min: 10, message: '长度不能少于10 个字符', trigger: 'change' },
{ validator: RegExpression.validateIP, trigger: "change" },
{ pattern: RegExpression.REG_Chinese_English_Digit, message: RegExpression.REG_Chinese_English_Digit_Message, trigger: "change" },
{ pattern: RegExpression.REG_Chinese_English_Digit,message: RegExpression.REG_Chinese_English_Digit_Message, trigger: "blur"}
]
地址:https://www.jianshu.com/p/ce451ef75fa0
Vue.directive('integer', { inserted: function (el) { el.addEventListener("keypress", function (e) { e = e || window.event; let charcode = typeof e.charCode == 'number' ? e.charCode : e.keyCode; if (e.key == 0 && !e.target.value) { e.preventDefault(); } let re = /\d/; if (!re.test(String.fromCharCode(charcode)) && charcode > 9 && !e.ctrlKey) { if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; } } }); } });
Vue.directive('floatInteger', { inserted: function (el) { el.addEventListener("keypress", function (e) { e = e || window.event; let charcode = typeof e.charCode == 'number' ? e.charCode : e.keyCode; let re = /\d/; if (charcode == 46) { if (el.value.includes('.')) { e.preventDefault(); } return; } else if (el.value.includes('.') && el.value.split('.')[1].length > 1) { e.preventDefault(); } else if (!re.test(String.fromCharCode(charcode)) && charcode > 9 && !e.ctrlKey) { if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; } } }); } });
<input
v-integer
type="number"
style="width:40%"
v-model="a"
/>
let hasPoint = false let pointNum = 2 const VueDirectiveObject = { findInput: function(el) { return el.classList.contains('el-input') ? el.querySelector('input') : el }, keepNumber: function(event) { var hasPoint = this.hasPoint var pointNum = this.pointNum this.value = hasPoint ? this.value.replace(/[^0-9.]/g, '').replace(/\./, '*').replace(/\./g, '').replace(/\*/, '.').replace(/^\./, '0.').replace(new RegExp('^(\\d+)\\.(\\d{' + Math.abs(pointNum) + '}).*$'), '$1.$2') : this.value.replace(/\D/g, '').replace(/\./g, '') }, keepNumberAndLetter() { this.value = this.value.replace(/[^a-zA-Z0-9]/g, '') }, onkeydown: function(event) { if (!(event.keyCode === 46) && !(event.keyCode === 8) && !(event.keyCode === 37) && !(event.keyCode === 39) && !(event.keyCode === 9) && !(event.key === '.' && this.hasPoint) && !(event.keyCode === 13) && !((event.keyCode === 67 || event.keyCode === 86 || event.keyCode === 65 || event.keyCode === 88) && event.ctrlKey && !event.altKey && !event.shiftKey) ) { // 数字 小键盘数字 if (!((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105))) { VueDirectiveObject.keepNumber.apply(this, event) event.stopPropagation() event.preventDefault() } } } }
Vue.directive('numberOnly', { bind: function(el, binding, vnode) { // 判断是否需要小数点 const arg = binding.arg || '' const el2 = VueDirectiveObject.findInput(el) hasPoint = arg.indexOf('point') !== -1 pointNum = parseInt(arg.replace('point', ''), 10) || 2 if (el2) { el2.hasPoint = hasPoint el2.pointNum = pointNum el2.style.imeMode = 'disabled' el2.addEventListener('input', VueDirectiveObject.keepNumber) el2.addEventListener('keydown', VueDirectiveObject.onkeydown) el2.addEventListener('keyup', VueDirectiveObject.keepNumber) el2.addEventListener('blur', VueDirectiveObject.keepNumber) } }, unbind: function(el) { let fel = VueDirectiveObject.findInput(el) fel.removeEventListener('input', VueDirectiveObject.keepNumber) fel.removeEventListener('keydown', VueDirectiveObject.onkeydown) fel.removeEventListener('keyup', VueDirectiveObject.keepNumber) fel.removeEventListener('blur', VueDirectiveObject.keepNumber) } })
Vue.directive('onlyNumberAndLetter', { bind: function(el, binding) { // 判断是否需要小数点 const el2 = VueDirectiveObject.findInput(el) if (el2) { el2.style.imeMode = 'disabled' el2.addEventListener('input', VueDirectiveObject.keepNumberAndLetter) el2.addEventListener('keyup', VueDirectiveObject.keepNumberAndLetter) el2.addEventListener('change', VueDirectiveObject.keepNumberAndLetter) el2.addEventListener('blur', VueDirectiveObject.keepNumberAndLetter) } }, unbind: function(el) { let fel = VueDirectiveObject.findInput(el) fel.removeEventListener('input', VueDirectiveObject.keepNumberAndLetter) fel.removeEventListener('keyup', VueDirectiveObject.keepNumberAndLetter) fel.removeEventListener('change', VueDirectiveObject.keepNumberAndLetter) fel.removeEventListener('blur', VueDirectiveObject.keepNumberAndLetter) } })
let fieldsToValidate = ['username', 'password', 'gender', 'email']; let _self = this; Promise.all(fieldsToValidate.map(item => { let p = new Promise(function (resolve, reject) { _self.$refs['form'].validateField(item, (error) => { resolve(error) }) }); return p; })).then((data) => { console.info(data) // data 里是各个字段的验证错误信息, 如果为空串则认为验证通过, 如果数组里全为空串则所有验证通过 // 判断data 里是否全是空串 // 去除数组空值 data = data.filter(item => item) if(!data.length){ console.log('通过验证') } else { console.log('验证不通过') } })
正则表达式–
验证手机号码:13[0-9]{9}
实现手机号前带86或是+86的情况:^((+86)|(86))?(13)\d{9}$
电话号码与手机号码同时验证:(^(\d{3,4}-)?\d{7,8})$|(13[0-9]{9})
提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) = (’|")?(\w|\|/|.)+(’|"| |>)?
提取信息中的邮件地址:\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)
提取信息中的图片链接:(s|S)(r|R)(c|C) = (’|")?(\w|\|/|.)+(’|"| |>)?
提取信息中的IP地址:(\d+).(\d+).(\d+).(\d+)
提取信息中的中国手机号码:(86)013\d{9}
提取信息中的中国固定电话号码:((\d{3,4})|\d{3,4}-|\s)?\d{8}
提取信息中的中国电话号码(包括移动和固定电话)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。