2

I have to control the textarea at the validation of the screen. When a line contains more than 78 characters or if you have more than 5 lines entered in the textarea, I have to stay on the screen and display an error message.

<body>
  <form name="newForm">
  <textarea id="myArea" cols="79" rows="5" onkeypress="upload(event);"></textarea>
  </form>
</body>

<script>
var counter1 = 0;
var counterrow = 0;
var old = 0;
var limit = 78;

function upload(e) {
  old = counter1;
  var myVal = document.getElementById("myArea");
  counter1 = myVal.value.length + 1;
  var code = (e.keyCode ? e.keyCode : e.which);
  if (counter1 <= 78) {
    counter1 = counter1 - (counterrow * limit);
  }

  if (counter1 <= limit && counterrow <= 5) {
    if (code == 13) { //Enter keycode
      counterrow++;
      counter1 = 0;
    }
  } else if (counter1 > limit && counterrow <= 5) {
    alert('cant xceed 78 Char/row : validations');
   if (code == 13) { //Enter keycode
      counterrow++;
      counter1 = 0;
    }
  } else if (counterrow > 5) {
    alert('cant exceed 5 : validation');
  }

}
</script>
Altay Mazlum
  • 442
  • 5
  • 15
simba
  • 277
  • 4
  • 19
  • Where's the problem? What errors occur? Please specify what you need help with. – tobifasc Aug 11 '15 at 10:20
  • Few things which i want to get are : 1.)I need an error message when a line exceed 78 characters or it exceed 5 row. 2.)When i am trying to reset the counter after 78 characters by subtracting length the rows entered above that, it gives a wrong calculation.It sets the counter in negative value. – simba Aug 11 '15 at 10:29
  • Yeah i got that. But i was wondering what the problem was with the code you postet. – tobifasc Aug 11 '15 at 10:30
  • Problem is, you can't really know how many lines are there. You can use parts of my code in [this answer](http://stackoverflow.com/a/4722395/447356) to find it. – Shadow The GPT Wizard Aug 11 '15 at 10:59

0 Answers0