感觉写的有问题,给你一个例子
<script type="text/javascript">
String.prototype.trim= function(){    
    return this.replace(/(^\s*)|(\s*$)/g, "");  
}
function validator(){
var name=document.getElementById("name");
if(name.value.trim()==""){
alert("name不能为空");
}
}
</script><form ... >
<input type="text" id="name" name="name" />
<input type="button" id="btn" value="ok" onclick="validator()" />
</form>看看javascript的dom方法吧