发新话题
打印

用javascript验证是否为空的问题。。。

用javascript验证是否为空的问题。。。

/*判断用户输入是否为空*/
function   isEmpty(ui){
if(ui=="" ||  ui==null){
  return false;
}
}

大家看这个函数有啥问题吗。。?

我调用的时候,无论你是不是为空,他都会返回false。。。好奇怪。。。

if(!isEmpty(value)){
   alert("its not allow null");
}
真邪门。。。
CodeIgniter,JQuery,Ext
http://www.girlsgroup.cn

TOP

首先你这个函数的命名是 isEmpty,所以当为空时应当返回 TRUE。
复制内容到剪贴板
JS 代码:
function isEmpty(input) {
  if (input == "" || input == null) {
    return true;
  }
  else {
    return false;
  }
}
Fred Wu
thislab.com

TOP

恩。。你这样写,的确很规范。。。但是我认为我的写法也没错呀。。。。

关键是调用的时候
if(!isEmpty(value)){
   alert("its not allow null");
}
这样调用。。。
CodeIgniter,JQuery,Ext
http://www.girlsgroup.cn

TOP

回复 2楼 的帖子

啊。。謝謝啦。。原來是我写法不规范的问题。。。。
CodeIgniter,JQuery,Ext
http://www.girlsgroup.cn

TOP

MS在javascript中
undefined==0
undefined==false
false==null

判断这类值时应该用绝对等于
===

TOP

发新话题