sam 发表于 2008-1-18 15:39:03

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

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

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

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

if(!isEmpty(value)){
   alert("its not allow null");
}
真邪门。。。

沧蓝 发表于 2008-1-18 16:26:42

首先你这个函数的命名是 isEmpty,所以当为空时应当返回 TRUE。

function isEmpty(input) {
if (input == "" || input == null) {
    return true;
}
else {
    return false;
}
}

sam 发表于 2008-1-18 16:40:57

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

关键是调用的时候
if(!isEmpty(value)){
   alert("its not allow null");
}
这样调用。。。

sam 发表于 2008-1-18 16:49:12

回复 2楼 的帖子

啊。。謝謝啦。。原來是我写法不规范的问题。。。。:)

iptton 发表于 2008-3-16 20:52:07

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

判断这类值时应该用绝对等于
===
页: [1]
查看完整版本: 用javascript验证是否为空的问题。。。