【问题标题】:Validation of textarea验证文本区域
【发布时间】:2010-03-26 06:54:39
【问题描述】:

如何验证表单中的文本区域?即,它不应为空或有任何新行,如果是,请发出警报。

代码:

   <script>
    function val()
    {
      //ifnewline found or blank raise an alert
    } 
   </script>
   <form>  
   <textarea name = "pt_text" rows = "8" cols = "8" class = "input" WRAP ></textarea>
   <input type=""button" onclick="val();"
    </form>

【问题讨论】:

  • 如果您不想要任何新行,为什么不使用文本字段?

标签: javascript html forms textarea dom-events


【解决方案1】:

我能想到的最简单的方法:

function validate() {
    var val = document.getElementById('textarea').value;

    if (/^\s*$/g.test(val) || val.indexOf('\n') != -1) {
        alert('Wrong content!');
    }
}

【讨论】:

    【解决方案2】:

    试试这个:

    <textarea id="txt" name = "pt_text" rows = "8" cols = "8" class = "input" WRAP ></textarea>
    
    function val()
    {
      if (trimAll(document.getElementById('txt').value) === '')
      {
         alert('Empty !!');
      }
    } 
    
    function trimAll(sString)
    {
        while (sString.substring(0,1) == ' ')
        {
            sString = sString.substring(1, sString.length);
        }
        while (sString.substring(sString.length-1, sString.length) == ' ')
        {
            sString = sString.substring(0,sString.length-1);
        }
    return sString;
    }
    

    【讨论】:

      【解决方案3】:

      这是一个简单的验证方法:

      function validate() {
          var val = document.getElementById('textarea').value;
          if (/^\s*$/g.test(val)) {
              alert('Wrong content!');
          }
      }
      

      And demo.

      【讨论】:

        【解决方案4】:
        <script>
            function val()
            {
              if(document.getElementById("textAread_id").value==null || document.getElementById("textAread_id").value=="")
        alert("blank text area")
            } 
           </script>
           <form>  
           <textarea id="textAread_id" name = "pt_text" rows = "8" cols = "8" class = "input" WRAP ></textarea>
           <input type=""button" onclick="val();"
            </form>
        

        【讨论】:

          【解决方案5】:

          首先给你的 textarea 一个唯一的标识符,这样你就可以很容易地获得对它的引用:

          然后你可以测试它是否包含一个新行或者它是空的,像这样:

          function val() {
              var el = document.getElementById('pt_text');
              if (el == null) {
                  // no element with given id has been found
                  return;
              }
              var value = el.value;
              if (value == null || value === '' || value.indexOf('\n') > 0) {
                  alert('empty or contains a new line');
              }
          }
          

          【讨论】:

          • 注意value.indexOf('\n') &gt; 0),indexOf 如果找不到则返回 -1,如果文本中的第一个字符是 则返回 0换行。
          【解决方案6】:
          <html>
          <head>
          <script type="text/javascript">
             function val(value){
                if(value.length == 0)
                   alert("thsi is empty");
             }
          </script>
          </head>
          <body>
          <textarea id="text"></textarea>
          <button onclick="val(text.innerHTML);">Check</button>
          </body>
          </html>
          

          这是检查textarea是否为空

          【讨论】:

            猜你喜欢
            • 2011-07-04
            • 2011-12-25
            • 1970-01-01
            • 1970-01-01
            • 2017-03-25
            • 2012-09-12
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多