我想问的是注册页面的用户名注册中间要辨别用户输入是否合法,密码两次是否正确,用js写怎么弄?如果不合法在text后面提示错误信息怎么写?
不喜欢alert,想要就在输入框后面提示。
求大神指导,刚学js。

解决方案 »

  1.   


    function regedit(){
    if(document.getElementById("userName").value.length == 0){
    document.getElementById("userNameNotice").innerHTML = "the userName can not be null!";
    return false;
    }
    if(document.getElementById("userPwd").value.length == 0){
    document.getElementById("userPwdNotice").innerHTML = "the userPwd can not be null!";
    return false;
    }
    if(document.getElementById("userPwd1").value.length == 0){
    document.getElementById("userPwdNotice1").innerHTML = "the userPwd1 can not be null!";
    return false;
    }
    if(document.getElementById("userPwd").value != document.getElementById("userPwd1").value){
    document.getElementById("pwd").innerHTML = "the two passwords you entered do not match!";
    return false;
    }
    }
    ------------------------------------------------------------------------------------
    <form action="" method="post">
       <table>
       <tr>
       <td>userName:</td>
       <td><input type="text" name="userName" id="userName" value=""/></td>
       <td><font color="red" id="userNameNotice"></td>
       </tr>
       <tr>
       <td>userPwd:</td>
       <td><input type="password" name="userPwd" id="userPwd" value=""/></td>
       <td><font color="red" id="userPwdNotice"></td>
       </tr>
       <tr>
       <td>userPwd again:</td>
       <td><input type="password" name="userPwd1" id="userPwd1" value=""/></td>
       <td><font color="red" id="userPwdNotice1"></td>
       <td><font color="red" id="pwd"></td>
       </tr>
       <tr>
       <td colspan="2">
       <input type="button" value="regedit" onclick="regedit()">
       </td>
       </tr>
       </table>
      
       </form>
      

  2.   

    加个隐藏域,如果不对,就document.getElementById("hiddenId").innerHTML="xxxxxxxx";
      

  3.   

    jQuery  很好很强大的。 下面是js的   很不好用
     <script type="text/javascript">
       function mcheck(){
       var name = document.getElementById("uname").value;
       if(null == name || "" == name){
       document.getElementById("span0").innerHTML = "name is null";
       }
       }
      </script>
      <body>
        name:<input id="uname" name="uname" onblur="mcheck()"><span id="span0"  style="color:red">*</span></br>
           
      </body>  
      

  4.   

     <script type="text/javascript" src="js/jquery-1.4.2.js"></script></head>
      <script type="text/javascript">
       function mcheck(){
       var name = $("uname").value;
       if(null == name || "" == name){
       $("#span0").html("name is null");
       }
       }
      </script>
      <body>
       文本框失去焦点
        name:<input id="uname" name="uname" onblur="mcheck()"><span id="span0" style="color:red">*</span></br>
      </body>
      

  5.   

    可以用onsubmit事件 就可以了
    事件关联的方法里面写具体的操作js
    就可以了
      

  6.   

    jquery.validate.js 你可以试试。呵呵
      

  7.   

    推荐个验证框架给你:Form validation,自己google下,效果好,简单易用!
      

  8.   

    动不动就jquery,jquery没出生前这样的问题就没法解决了?
      

  9.   

    你可以先定义一下div,然后正如4楼写的,但是改造下,如果出错,你可以show出这个div,给这个div加上一个text,提示自己的错误信息。