It is required to count the number of characters in the input and textarea text fields and if the text is more than acceptable, then highlight the field background in red or yellow (it is desirable that the text that is added to the text field is not clipped, if it is large and just highlighted in red).

    2 answers 2

    I recently downloaded a similar script, I think it will suit you and in zzhatom form takes less than 1 KB. Here is a link to this script. You need to connect the script itself and add such code to the page:

     $(document).ready(function() { $('[data-countchar]').countChar({ text: 'Characters:' }); }); 

    Html:

     <input type="text" name="name" data-countchar="[az]+" placeholder="тСкст"> <input type="text" name="age" data-countchar="[\d]+" placeholder="Ρ†ΠΈΡ„Ρ€Ρ‹"> <textarea name="desc" data-countchar="" data-countchar-limit="15" placeholder="ΠœΠ°ΠΊΡΠΈΠΌΡƒΠΌ 15 символов"></textarea> 

    data-countchar-limit="15" = maximum number of characters (15).

    data-countchar="[az]+" = which characters to count (az).

      Instead of change you can use input if you need instant response. Now the script works when the item becomes inactive.

       $(document).ready(function(){ var input = $('.input'); var textarea = $('.textarea'); $(document).on('change','.input',function(){ if(input.val().length < 6) { // Ссли < 6 символов input.css('background','#FF0000'); return false; } else { input.css('background','#FFFFFF'); return true; } }); $(document).on('change','.textarea',function(){ if(textarea.val().length < 6) { // Ссли < 6 символов textarea.css('background','#FF0000'); return false; } else { textarea.css('background','#FFFFFF'); return true; } }); }); 
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="input" type="text" name="text"><br /><br /> <textarea class="textarea" name="textarea"></textarea> 

      • And how to make this script count for several fields separately? Those. each field has its own counter, and not one for all fields - Dikkiy