最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Validation of textarea - Stack Overflow

programmeradmin3浏览0评论

How to do I validate a textarea in a form? i.e, it should not be empty or have any new lines, and if so raise an alert.

Code:

   <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>

How to do I validate a textarea in a form? i.e, it should not be empty or have any new lines, and if so raise an alert.

Code:

   <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>
Share Improve this question edited Aug 21, 2019 at 18:54 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Mar 26, 2010 at 6:54 HulkHulk 34.2k64 gold badges150 silver badges217 bronze badges 1
  • 1 if you dont want any new lines, why dont you just use a text field? – kdureidy Commented Jun 22, 2013 at 8:57
Add a comment  | 

6 Answers 6

Reset to default 2

The easiest way I could think of:

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

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

Try this:

<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;
}

Here is a simple way for validation:

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

And demo.

<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>

First give your textarea an unique identifier allowing you to easily obtain reference to it:

Then you could test if it contains a new line or it is empty like so:

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');
    }
}
<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>

This is to check if the textarea is empty

发布评论

评论列表(0)

  1. 暂无评论