不能发送空白信息
方法一:把所有 空白字符 转为 空字符串 。
const str = " "
const str2 = " cez "
const isEmpty = str.replace(/\s/g, "")
const isEmpty2 = str2.replace(/\s/g, "")
console.log(str.length) // 4 " "
console.log(isEmpty.length) // 0 ""
console.log(isEmpty2.length) // 3 "cez"
if (!isEmpty) {
console.log("空白信息,不能发送!!!")
}
if (isEmpty2) {
console.log("非空白信息,可以发送...");
}
方法二:使用 jQuery
的静态方法 trim()
去除掉字符串起始和结尾的空格。
const str = " "
const str2 = " cez "
const isEmpty = $.trim(str)
const isEmpty2 = $.trim(str2)
console.log(str.length) // 4 " "
console.log(isEmpty.length) // 0 ""
console.log(isEmpty2.length) // 3 "cez"
if (!isEmpty) {
console.log("空白信息,不能发送!!!")
}
if (isEmpty2) {
console.log("非空白信息,可以发送...")
}