I need to validate textarea in a manner that there can be maximum 3 lines and each line may contain maximum of 20 characters.
I have in included jQuery library.
I need to validate textarea in a manner that there can be maximum 3 lines and each line may contain maximum of 20 characters.
I have in included jQuery library.
check this out:
<textarea id="ta" rows="10" cols="50"></textarea>
$(function(){
var ta = $('#ta');
ta.keyup(function(){
var flag = !0;
var arr = this.value.split(/\n/);
if(3 < arr.length){
flag = !1;
} else {
$.map(arr, function(val, idx){
if(20 < val.length){
flag = !1;
return false;
}
});
}
if(!flag){ // invalid
ta.css('backgroundColor', '#fcc');
} else {
ta.css('backgroundColor', '#fff');
}
});
});
Maximum 3 lines. every line maximum is 20 characters
var t=document.getElementById('textAreaId').value;
if(/^(?:[^\n]{0,20}\n?){0,3}$/g.test(t) !== true){
alert('input is invalid');
}
or using jQuery:
$(function () {
$('textarea').on("input", function () {
var valid = /^(?:[^\n]{0,20}\n?){0,3}$/g.test( this.value );
$(this).css({background: valid ? "white" : "red"});
});
});