JavaScript Validation for multiple textarea in javascript jquery validation multiple textarea

I have a page which has 2 text areas and wanted to validate each text area if the characters being typed is more than max length upon clicking submit button. Problem with the code below when clicking submit it can only detect if textarea1 is more than max length but not the other textarea. What I wanted to do is it can determine which textarea is exceeding maxlength so users can correct it. Thanks in advance.

 <html>
<head>

<script>
function testing()
{
    var txtlength=document.getElementById("txt1").value.length;
    if(txtlength>50)  
    {
        alert("Max1 length reached ");
        return false; 
    }
    else
    {
        return true; 
    }

    var txtlength1=document.getElementById("txt2").value.length;
    if(txtlength1>50)  
    {
        alert("Max2 length reached ");
        return false;
    }
    else
    {
        return true;
    }

}
</script>
</head>
<body>
<form action="test1.html" method="post" onsubmit="return testing();">
    <textarea id="txt1"></textarea>
    <textarea id="txt2"></textarea>
    <input type="submit" value="Submit"/>
</form>
</body>
</html>
Answer:1

the return true statement should only be done once, at the end of the code.

the final code should looks something like this :

<script>
function testing()
{
    var txtlength=document.getElementById("txt1").value.length;
    if(txtlength>50)  
    {
        alert("Max1 length reached ");
        return false; 
    }

    var txtlength1=document.getElementById("txt2").value.length;
    if(txtlength1>50)  
    {
        alert("Max2 length reached ");
        return false;
    }

    return true;
}
</script>           
Answer:2
function testing()
{
    var txtlength=document.getElementById("txt1").value.length;    
    var txtlength1=document.getElementById("txt2").value.length;
    if(txtlength>50)  
    {
        alert("Max1 length reached ");
        return false; 
    }
    else if(txtlength1>50)  
    {
        alert("Max2 length reached ");
        return false;
    }
        return true;  
}
Answer:3

Change your following function:

var txtlength=document.getElementById("txt1").value.length;
    if(txtlength>50)  
    {
        alert("Max1 length reached ");
        return false; 
    }
    else
    {
        return true; 
    }

    var txtlength1=document.getElementById("txt2").value.length;
    if(txtlength1>50)  
    {
        alert("Max2 length reached ");
        return false;
    }
    else
    {
        return true;
    }

to:

var txtlength=document.getElementById("txt1").value.length;
        if(txtlength>50)  
        {
            alert("Max1 length reached ");
            return false; 
        }
        else
        {
            var txtlength1=document.getElementById("txt2").value.length;
            if(txtlength1>50)  
            {
            alert("Max2 length reached ");
            return false;
            }
            else
            {
            return true;
            }
        }
Answer:4

Rather than hard–coding the form ID, pass a reference to the from from the listener:

<form ... onsubmit="return testing(this);">

In the function, loop over the form's controls and check them:

function testing(form) {
  var control;

  for (var i=0, iLen=form.elements.length; i<iLen; i++) {
    control = form.elements[i];

    if (control.tagName.toLowerCase() == 'textarea' && control.value.length > 50) {
       alert(control.name + ' must be 50 characters or less');
       return false;
    }
  }
}

}
Answer:5

first of all I apologize for my bad English, I want to make a JavaScript for a tabele who makes himself automatically by the ID Bold, just like this one here but the code is not only for weeks for ...

first of all I apologize for my bad English, I want to make a JavaScript for a tabele who makes himself automatically by the ID Bold, just like this one here but the code is not only for weeks for ...

On my webpage, I set my pdf as follow: <object data="myURL" type="application/pdf" width="300" height="200"></object> where the URL is created with javascipt from a blob like this: var ...

On my webpage, I set my pdf as follow: <object data="myURL" type="application/pdf" width="300" height="200"></object> where the URL is created with javascipt from a blob like this: var ...

  1. embedded save to flash
  2. save html with embedded images
  3. save pdf with embedded fonts
  4. save powerpoint with embedded fonts
  5. save powerpoint with embedded video
  6. save ppt with embedded fonts
  7. save powerpoint with embedded fonts mac
  8. save word with embedded objects as a pdf
  9. save powerpoint with embedded media
  10. save pdf with embedded documents
  11. save ppt with embedded video
  12. save pdf with embedded files
  13. save word with embedded fonts

I have been working on this for a while and would love some help. I have two sets of divs of different lengths offset by varying distances which are being created from an array. Fiddle 1: JSFiddle1 ...

I have been working on this for a while and would love some help. I have two sets of divs of different lengths offset by varying distances which are being created from an array. Fiddle 1: JSFiddle1 ...

as you can see here jsfiddle.net/TTU4Z/1/ The jquery code in question is below and if i run it without all my code (like in jsfiddle) it run perfectly. but with all my code doesn't run. $(document)....

as you can see here jsfiddle.net/TTU4Z/1/ The jquery code in question is below and if i run it without all my code (like in jsfiddle) it run perfectly. but with all my code doesn't run. $(document)....

  1. plus minus button excel
  2. plus minus button html
  3. plus minus button javascript
  4. plus minus button on calculator
  5. plus minus button on gear shifter
  6. plus minus button on acadia shifter
  7. plus minus button in android
  8. plus minus button bootstrap
  9. plus minus button jquery
  10. plus minus button angular
  11. plus minus button codepen
  12. plus minus button php
  13. plus minus button w3schools
  14. plus minus button in bootstrap 4