textBox 为空的或者字数大于30个字 提示内容为空和字数大于30个字

解决方案 »

  1.   

    if(string.IsNullOrEmpty(textBox.Text))
    {
      //内容为空
    }
    else if(textBox.Text.Length>30)
    {
    //字数大于30个字
    }
      

  2.   

    if(textBox.Text.Length==0)
    //提示内容为空
    if(textBox.Text.Length>30)
    //提示字数大于30个字
      

  3.   

    if(textbox.text=="标题名")
    {
    response.write("alert('标题名不能重复')");
    }
      

  4.   

    用JQuery来实现:<script type="text/javascript">  
             $(document).ready(function () {  
                 $("#error").hide();  
                $("#Button1").click(function () {  
                     var $val = $("#Text1").val();  
                     var code;                   if(code.length > 30 || code.length==0){
                             $("#error").show();  
                             break; 
                        }
                    
                     for (var i = 0; i < $val.length; i++) {  
                         //charAt()获取指定位置字符串,charCodeAt()返回该字符串的编码  
                            //0的ASCII是48,9的ASCII是57  
                         var code = $val.charAt(i).charCodeAt(0);  
                         if (code < 48 || code > 57) {  
                             $("#error").show();  
                             break;  
                         }  
                         else {  
                             $("#error").hide();  
                         }  
                     }  
                 });  
             });  
         </script>  
    意思差不我就是这个样子了!
    如果你不用这个button 去实现,你可以让鼠标 onmousemoveout 事件来实现验证!
      

  5.   

    最好先Trim一下,不然按几下空格键,也算内容不为空,容易出错。
    .net 4.0里提供了String.IsNullOrWhitespace方法,就省去Trim的一步了
      

  6.   

    if(string.IsNullOrEmpty(textBox1.Text))
    {
    }
    else if(textBox1.Text.Length>30)
    {
    }
      

  7.   

    public string CheckData(string textBoxData){
    if(string.IsNullOrEmpty(textBoxData)||textBoxData.Length>30)
    {  
       StringBuilder errorMSG=new StringBuilder();
       if(string.IsNullOrEmpty(textBoxData)){
          //内容为空
           errorMSG.append("内容为空");
       }
       if(textBoxData.Length>30){
          //提示字数大于30个字
           errorMSG.append("提示字数大于30个字");
       }
    }
    return errorMSG.ToString();
    }