textarea - prevent user to type more lines than specified

My textarea is rows = "3" and need to prevent user to type more than 3 lines.

This code prevents to type into fourth line but the line is set anyway.

How to prevent entering new line by pressing Enter if three lines are already there?

$('.tx').on('keypress', function () {
   let max = parseInt($(this).attr('rows'));
   let a = $(this).val();
   let x = a.split("\n").length - 1;
   if(x == max){return false;}
});
.tx{
display:block;
width:100%;
background:gold;
line-height:1.3em;
text-transform:uppercase;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class='tx' rows = "3"></textarea>


#javascript #jquery

1 Likes2.35 GEEK