123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- import Vue from 'vue'
- // 手机号验证
- var mobile = (rule, value, callback) => {
- let regFormat = /^[1][3456789][0-9]{9}$/ //正确手机号
- if (!value) {
- return callback(new Error('不能为空'))
- }
- if (!(regFormat.test(value))) {
- callback(new Error('请输入正确手机号'))
- } else {
- callback()
- }
- }
- //数字验证
- var number = (rule, value, callback) => {
- if (!value) {
- return callback(new Error('不能为空'))
- }
- if (!Number.isInteger(value)) {
- callback(new Error('请输入数字值'))
- } else {
- if (value < 18) {
- callback(new Error('必须大于18岁'))
- } else if (value > 55) {
- callback(new Error('必须小于55岁'))
- } else {
- callback()
- }
- }
- }
- //text不能为空
- var text = (rule, value, callback) => {
- if (!value) {
- return callback(new Error('不能为空'))
- } else {
- callback()
- }
- }
- //desc不能为空
- var desc = (rule, value, callback) => {
- if (!value) {
- return callback(new Error('不能为空'))
- } else {
- callback()
- }
- }
- //邮箱
- var email = (rule, value, callback) => {
- let mal = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
- if (!value) {
- return callback(new Error('不能为空'))
- }
- if (!(mal.test(value))) {
- callback(new Error('请输入正确邮箱'))
- } else {
- callback()
- }
- }
- //开始时间
- var startTime = (rule, value, callback) => {
- if (!value) {
- return callback(new Error('请选择开始日期'))
- } else {
- callback()
- }
- }
- //开始时间
- var endTime = (rule, value, callback) => {
- if (!value) {
- return callback(new Error('请选择结束日期'))
- } else {
- callback()
- }
- }
- //时间
- var time = (rule, value, callback) => {
- if (!value) {
- return callback(new Error('请选择日期'))
- } else {
- callback()
- }
- }
- //多选框
- var checkbox = (rule, value, callback) => {
- if (value.length < 1) {
- return callback(new Error('请选择一个'))
- } else {
- callback()
- }
- }
- //单选框
- var radio = (rule, value, callback) => {
- if (value.length < 1) {
- return callback(new Error('请选择一个'))
- } else {
- callback()
- }
- }
- //下拉框
- var select = (rule, value, callback) => {
- if (value.length < 1) {
- return callback(new Error('请选择一个'))
- } else {
- callback()
- }
- }
- //QQ号码验证
- var QQV = (rule, value, callback) => {
- if (value === '') {
- callback(new Error('输入QQ号'))
- } else if (/^[1-9][0-9]{4,10}$/.test(value)) {
- callback()
- } else {
- callback(new Error('输入正确的QQ号'))
- }
- }
- // 类似金钱,首位不为0,最多2位小数
- function checkNumPot2 (rule, value, callback) {
- const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
- if (!value) {
- return callback(new Error('请填写数字'))
- } else if (!reg.test(value)) {
- return callback(new Error('请填写数字,最多2位小数'))
- } else {
- callback()
- }
- }
- // 身份证
- function checkIdNum (rule, value, callback) {
- const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
- if (!value) {
- return callback(new Error('证件号码不能为空'))
- } else if (!reg.test(value)) {
- return callback(new Error('证件号码不正确'))
- } else {
- callback()
- }
- }
- // 整数
- function checkInterNum (rule, value, callback) {
- const reg = /^[0-9]*[1-9][0-9]*$/
- if (!value) {
- return callback(new Error('请填写整数'))
- } else if (!reg.test(value)) {
- return callback(new Error('请输入整数'))
- } else {
- callback()
- }
- }
- const confirmPasswordValidator = (rule, value, callback, password) => {
- if (password !== value) {
- callback(new Error('两次密码输入不一致'))
- } else {
- callback()
- }
- }
- const formRules = {
- QQ: [{ required: true, validator: QQV, trigger: 'blur' }],
- mobile: [{ required: true, pattern: /^1[23456789]\d{9}$/, message: '目前只支持中国大陆的手机号码', trigger: 'blur' }],
- password: [{ required: true, min: 6, max: 18, message: '长度必须大于6位长度在 6 到 18 个字符之间', trigger: 'blur' }],
- confirmPassword: (form) => {
- return [
- { required: true, min: 6, max: 18, message: '长度必须大于6位长度在 6 到 18 个字符之间', trigger: 'blur' },
- {
- required: true,
- validator: (rule, value, callback) => confirmPasswordValidator(rule, value, callback, form.password),
- trigger: 'blur'
- }
- ]
- },
- required: [{ required: true, min: 1, message: '必填', trigger: 'blur' }],
- numPot2: [{ required: true, validator: checkNumPot2, trigger: 'blur' }],
- InterNum: [{ required: true, validator: checkInterNum, trigger: 'blur' }],
- coins: [{ required: true, validator: checkInterNum, trigger: 'blur' }]
- }
- Vue.prototype.formRules = formRules
|