Skip to content

Instantly share code, notes, and snippets.

@reachkamrul
Created November 20, 2021 10:10
Show Gist options
  • Save reachkamrul/eb4930ae83294e50ba4dbe8356033932 to your computer and use it in GitHub Desktop.
Save reachkamrul/eb4930ae83294e50ba4dbe8356033932 to your computer and use it in GitHub Desktop.
ThisForm = $('.maxChar'); // Give element class
min = 5; //Give minimum character number
max = 10; //Give maximum chartacter number
limitCrossed = "you have reached the limit"; // Text to show when limit crossed
goodToGo = "good to go"; // Text to show when the character length is perfect, use " " if you dont want to show any text
required = "characters required"; // Text to show minimum character required
ThisForm.after( "<p class='charLimit'></p>" );
ThisForm.on('keyup keypress',function () {
$(this).attr('maxlength',max)
$(this).attr('minlength',min)
var len = $(this).val().length;
if (len == 10) {
$('.charLimit').html(' '+limitCrossed);
}
if (min-1<len && len<max) {
$('.charLimit').text(goodToGo);
}
if(len<min) {
var char = min - len;
$('.charLimit').text(char + ' '+required);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment