验证文本区域的每一行

时间:2015-08-11 10:17:52

标签: javascript html

我必须在屏幕验证时控制textarea。 当一行包含超过78个字符或如果您在textarea中输入的行数超过5行时,我必须留在屏幕上并显示错误消息。

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

0 个答案:

没有答案