# Js判断为空、长度大于某个值、空格判断、包含某个字符判断

Js开发中常用的方法


判断undefined
  • typeof的运算符未定义则返回undefined
//判断undefined
function checkisUndefined(str) {
    var st = undefined;
    if (typeof (str) == "undefined") {
        alert("此对象是undefined!")
    } else {
        alert("此对象不是ubdefined!")
    }
}
判断输入的内容是否为空
//判断空值
function checkNullOrNotnull(st) {
    var str = st;
    if (typeof (str) == "undefined" || str == 0 || str == null) {
        alert("内容为空!")
    } else {
        alert("内容不为空!")
    }
}
判断输入内容长度大于一定值
//Js长度函数
function checkLength(st) {
    debugger;
    var lengthValue = st.length;
    if (lengthValue == 0) {
        alert("输入为空!")
    } else if (lengthValue > 20) {
        alert("长度超出20");
        document.getElementById("input1").innerHtml = "";
    }
}
判断输入的内容全部是空格
//判断输入是否含有空格
function checkNull(st) {
    debugger;
    var regu = "^[ ]+$";
    var re = new RegExp(regu);
    if (re.test(st)) {
        alert("全是空格")
    } else {
        alert("不全是空格")
    }
}
判断是否含有小数或者是负值
function checkXiaoshu(st) {
    debugger
    if (st < 0) {
        alert("是负数");
        return;
    } else if (st.indexOf("." != -1)) {
        alert("是小数")
    } else if (st.indexOf("." == -1)) {
        alert("不是小数")
    }
}

。。。未完待续

猜你喜欢

转载自blog.csdn.net/qq_37248504/article/details/107436358