zoukankan      html  css  js  c++  java
  • js判断输入字符串是否为空、空格、null的方法总结 阿星小栈

    判断字符串是否为空

       var strings = '';
       if (string.length == 0)
       {
        alert('不能为空');
       }

    判断字符串是否为“空”字符即用户输入了空格

    var strings = ' ';
    if (strings.replace(/(^s*)|(s*$)/g, "").length ==0)
    {
    alert('不能为空');
    }

    判断输入字符串是否为空或者全部都是空格

    function isNull( str ){
    if ( str == "" ) return true;
    var regu = "^[ ]+$";
    var re = new RegExp(regu);
    return re.test(str);
    }
    如果有null时上面代码就无法正常判断了,下面代码是判断为null的情况
    var exp = null;
    if (exp == null)
    {
    alert("is null");
    }
     
    exp 为 undefined 时,也会得到与 null 相同的结果,虽然 null 和 undefined 不一样。

    注意:要同时判断 null 和 undefined 时可使用本法。 代码如下

    var exp = null;
    if (!exp)
    {
    alert("is null");
    }

    如果 exp 为 undefined,或数字零,或 false,也会得到与 null 相同的结果,虽然 null 和二者不一样。注意:要同时判断 null、undefined、数字零、false 时可使用本法。代码如下

    var exp = null;
    if (typeof exp == "null")
    {
    alert("is null");
    }

    为了向下兼容,exp 为 null 时,typeof null 总返回 object,所以不能这样判断。

    function testuser(){
    var i= document.getElementByIdx_x("aa");
    if (i.value=="null")
    {
    alert("请登录后再发表留言!")
    return false;
    }
    else
    {
    alert(i.value)
    return true;
    }
    }

    以上这篇js判断输入字符串是否为空、空格、null的方法总结就是小编分享给大家的全部内容了,希望能给大家一个参考

    阿星小栈 - 博客园  

  • 相关阅读:
    weui-switch开关控件,表单提交后如何取值
    [转]判断存储过程、触发器、视图是否存在并删除
    修改服务器的3389端口
    如何在react-native 中优雅的使用 redux
    react native js 与 native 的通信与交互方式
    对 JS virtual DOM 的一知半解
    Python的实例方法,类方法,静态方法之间的区别及调用关系
    redux 管理你的 react 应用
    react- native 入门
    git 指南
  • 原文地址:https://www.cnblogs.com/dereckbu/p/7279494.html
Copyright © 2011-2022 走看看