I have the following code which works well, but the problem is that after exceeding the 500 characters it starts to allow the user to type (it accepts the characters instead of restricting them!).
How can I modify it? Is there any possibility to generalize this code so it can handle multiple text areas, like a function and just pass the parameters?
$('#txtAboutMe').keyup(function () {
var text = $(this).val();
var textLength = text.length;`enter code here`
if (text.length > maxLength) {
$(this).val(text.substring(0, (maxLength)));
alert("Sorry, you only " + maxLength + " characters are allowed");
}
else {
//alert("Required Min. 500 characters");
}
});"
You could try defining a maxLength to be used for comparing (if it's not defined is equal to undefined and every number is more than undefined: that's why you nevere get the alert i think):
$('#txtAboutMe').keyup(function () {
var maxLength = 500;
var text = $(this).val();
var textLength = text.length;
if (textLength > maxLength) {
$(this).val(text.substring(0, (maxLength)));
alert("Sorry, you only " + maxLength + " characters are allowed");
}
else {
//alert("Required Min. 500 characters");
}
});"