页面上有两个TextBox,我想通过JS对输入进行验证:如果第一个TextBox填写内容了,则第二个必须也要填写;同理,如果第二个填写了,同时也要求第一个必须填写;说白了就是:要么两个都填写,要么两个都不填请高人给个代码...

解决方案 »

  1.   

      function checkNull()
       {
          var t1=document.getElementById("textbox1");
          var t2=document.getElementById("textbox2");
          if(t1.value==""&&t2.value=="")
           {
               alert('不能空');
               return false;
          }
       }
      

  2.   


    function check()
    {
        var tb1 = document.getElementById("<%= TextBox1.ClientID %>");
        var tb2 = document.getElementById("<%= TextBox2.ClientID %>");
        if(tb1.value != "")
        {
           if(tb2.value == "")
           {
                alert("tb2不能为空");
                return false; 
           }
           return true;
        }
        else
        {
            if(tb2.value != "")
           {
                alert("tb1不能为空");
                return false; 
           }
           return true;
        }
    }
      

  3.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script language="javascript">
    function check(){
    if((document.getElementById('one').value=="")^(document.getElementById('two').value=="")){
    alert('false');
    }else
    {
    alert('true');
    }
    }
    </script>
    </head><body>
    <input type="text" id="one"/>
    <input type="text" id="two"/>
    <input type="button" onclick="check()" />
    </body>
    </html>
      

  4.   

    因为我的注册页面比较大,在点击提交Button时验证,然后弹出alert是很不友好的,能不能在输入第一个TextBox时候,紧接着就强制输入第二个TextBox