function check(form)
{
    if (!form.name.value.length)
    {
        alert('请输入用户名!');
        return false;
    }
    return true;
}
<form action="XXXXX" method="post" onSubmit="return check(this)">
..................
</form>
就是这个样子的

解决方案 »

  1.   

    我原来也碰到过类似的情况,但是上面那段程序没问题呀
    <script>
    function check(form)
    {
        if (!form.name.value.length)
        {
            alert('请输入用户名!');
            return false;
        }
        return true;
    }
    </script>
    <form action="XXXXX" method="post" onSubmit="return check(this)">
    <input type="text" id="name">
    <input type="submit" value="提交">
    </form>
      

  2.   

    if (!form.name.value.length)
    这个判断太不严谨了
    而且是错误的
      

  3.   

    最好不要用name作为名字或者id
      

  4.   

    to  xiaoshi(java入门中....)(半知不解)   
    为何这样if (!form.name.value.length)的语句是错误的?错误在哪里呢?
      

  5.   

    if( form.name.value == '' ){
        return false;
    }
      

  6.   

    改成这样看看:
    <form action="XXXXX" method="post">
    <input type="text" id="name">
    <input type="submit" value="提交" onclick="return check(this)">
    </form>
      

  7.   

    if( form.name.value == '' )这样判断是没错,但是,length属性是指其长度,为什么会错呢?value==''和value.length == 0又有什么区别呢?呵呵,好了,结贴。