关于表单提交判断不能为空的方法

使用方法

if(!isNull(obj, _this.form)) return;

obj是自己声明的一个对象,用来和提交的对象做对比

let obj = {
title: '标题',
explain: '滚动字幕',
imgurl: '封面图',
region: '地区',
industry: '行业',
subject: '主体',
business: '直播类型',
starttime: '开始时间',
endtime: '结束时间',
}

function isNull(txt, obj) {
for(var i in txt) {
var gettype = Object.prototype.toString;
if(gettype.call(obj[i]) == '[object Null]' || gettype.call(obj[i]) == '[object Undefined]') { //空
msg(txt[i] + '不能为空')
return false
}
if(gettype.call(obj[i]) == "[object Array]" || gettype.call(obj[i]) == "[object String]") { //数组或者字符串
if(obj[i].length == 0) {
msg(txt[i] + '不能为空')
return false
}
}
if(gettype.call(obj[i]) == "[object Object]") { //对象
var num = 0
for(var j in obj[i]) {
num++
}
if(num == 0) {
msg(txt[i] + '不能为空')
return false;
}
}
}
return true;
}

猜你喜欢

转载自www.cnblogs.com/zjxiang008/p/12106043.html