<script runat="server" type="text/javascript">
function checklogin()
 {
  var username=document.getElementById("TextBox1").Text;
  var pwd=document.getElementById("TextBox2").Text;
   
  var usern =/^[a-zA-Z0-9]$/;      //出错行
 
    
if(username.Length==0||pwd.Length==0){
    return false;
    Response.Write("您输入的用户名或密码为空");
}       
  if (!username.value.match(usern)||!pwd.value.match(usern)) {
      return false;
      Response.Write("您输入的字符格式不对");
               
   
        }
if(username.Length<6||username.Length>20||pwd.Length<6||pwd.Length>20){
  return false;
  Response.Write("您输入的字符长度不对");
}
    
}
</script>
正则表达式是这么写吗?网上看了好多资料都是这样写的啊 怎么我这里就老是报这个错呢?
求大虾~!!JavaScripttextbox正则表达式密码

解决方案 »

  1.   

    你这里是服务器端的C#,还是客户端的javascript?
      

  2.   

    客户端的话
    runat="server"要去掉
    <script runat="server" type="text/javascript"> Response.Write("您输入的用户名或密码为空"); Response.Write也不是javascript可以使用的。
      

  3.   

    咦我这个Response.Write肿么没报错呢
      

  4.   

    <script type="text/javascript">
     function checklogin()
      {
       var username=$("#TextBox1").val();
       var pwd=$("#TextBox2").val();
        
       var usern =/^[a-zA-Z0-9]$/;      //出错行
      
         
     if(username.Length==0||pwd.Length==0){
             alert("您输入的用户名或密码为空");
              return false; }
     
           
       if (!usern.test(username)||!usern.test(pwd)) {
          
           alert("您输入的字符格式不对");
          return false;
        
             }
     if(username.Length<6||username.Length>20||pwd.Length<6||pwd.Length>20){
      
       Response.Write("您输入的字符长度不对");
            return false;
     }
         
     }
     </script>
    PS:如果是客户端脚本,用jquery比较好,试试这段代码
      

  5.   

     Response.Write("您输入的字符长度不对");忘了一句,这句代码再换成 alert("您输入的字符长度不对");
      

  6.   


    LZ真厉害。
    你先学学 C#  或者 先学学javascript不能俩个一起学哦。
      

  7.   

    你是新手吧,好多語法錯誤,建議先看些JS基本入門的書。下頁是我幫你修改過的代碼function checklogin()
      {
      // var username=document.getElementById("TextBox1").Text;
       //var pwd=document.getElementById("TextBox2").Text; 
       var username=document.all.TextBox1.value; //文本框可以直接取值或 document.formname(你的表單名稱).TextBox1.value;
       var pwd=document.all.TextBox2.value;
      // var usern =/^[a-zA-Z0-9]$/;      //出错行
       var usern =/^[a-z][A-Z][0-9]$/;    //可以找些js的正規運算的資料看看
      
         
     if(username.Length==0||pwd.Length==0){
         //return false;
       //  Response.Write("您输入的用户名或密码为空");
           alert("您输入的用户名或密码为空"); //彈出提示框使用alert,不是使用response,那是頁面上的寫法,不是JS腳本
       return false; //此句要在后面,不然,提示框不出來
     }
     
           
      // if (!username.value.match(usern)||!pwd.value.match(usern)) {
      if(!usern.test(username)||)
           //return false;
           //Response.Write("您输入的字符格式不对");
       alert("用戶名格式不对,只能使用字母或數字組成"); //彈出提示框使用alert,不是使用response,那是頁面上的寫法,不是JS腳本
       return false; //此句要在后面,不然,提示框不出來
     }
      if(!usern.test(pwd)||)
           //return false;
           //Response.Write("您输入的字符格式不对");
       alert("密碼格式不对,只能使用字母或數字組成!"); //彈出提示框使用alert,不是使用response,那是頁面上的寫法,不是JS腳本
       return false; //此句要在后面,不然,提示框不出來
     }  
     if(username.Length<6||username.Length>20||pwd.Length<6||pwd.Length>20){
      // return false;
     // Response.Write("您输入的字符长度不对");
       alert("您您输入的字符长度不对");
       return false;
     }
     
     }
      

  8.   

    其实今天错误改过来了。。但是最终还是没有实现客户端的一个检测。
    把我的页面给你看下吧,
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    <center>请输入用户名<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </center>
    <center>请输入密码<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox></center>
    <center><asp:Button ID="Button1" runat="server" Text="登录"  OnClientClick="return checklogin()" OnClick="Button1_Click"></asp:Button>;
            <asp:Button ID="Button2" runat="server" Text="重置" OnClick="Button2_Click" ></asp:Button>
            </center>
        </div>
        </form>
    </body>
    </html>
    什么都不输入就是不能弹出输入为空的提示。直接进入button1_click事件中的"您输入的用户名密码错误"...
    大哥 麻烦你了,
      

  9.   

    下面的代碼我測試過的,你直接複製下來就可以運行了。並且是HTML的。
    <html>
    <head>
    <script language="javascript">
    function checklogin(){
       var username=document.all.TextBox1.value;
       var pwd=document.all.TextBox2.value;
       var usern =/^[a-z]|[A-Z]|[0-9]$/; //判斷是字母或數字 正則運算
     if(username==""){
       alert("請輸入用戶名!");
       document.all.TextBox1.focus();
       return false;
     }else{
     if(usern.test(username)==false){
       alert("用戶名必須由字母和數字組成!");
       document.all.TextBox1.focus();
       return false; 
    }else{
    if(username.length<6||username.length>20){
       alert("用戶名長度不正確,必須在6~20之間!");
       document.all.TextBox1.focus();
       return false; 
    }
    }  
     }
     if(pwd==""){
       alert("請輸入密碼!");
       document.all.TextBox2.focus();
       return false;
     }else{
     if(usern.test(pwd)==false){
       alert("密碼名必須由字母和數字組成!");
       document.all.TextBox2.focus();
       return false; 
    }else{
    if(pwd.length<6||pwd.length>20){
       alert("密碼長度不正確,必須在6~20之間!");
       document.all.TextBox2.focus();
       return false; 
    }
    }  
     }
    return true; 
     }
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=big5">
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post">
    <input id="TextBox1" name="TextBox1" type="text">
    <input id="TextBox2" name="TextBox2" type="password">
    <input type="button" name="Button" value="submit" onClick="checklogin()">
    </form>
    </body>
    </html>