Advertisement:

Author Topic: [TUTORIAL] Add characters Counter and limit in Item Title and Description -  (Read 4121 times)

zoopla

  • Guest
Hello everyone,
I have seen many post asking about how to limit item title and description to a specified number of characters,
Many of the other answers suggested modification of core files-
But in this tutorial I will explain how to achieve this  without modifying  any core files.

Also i have seen many requests for item counter so users have idea of how many characters they have left...

So here it is
In this tutorial I will explain how to make An item characters counter and how to limit the number of characters allowed   in bender theme - but it should be similar in most of the other themes.


First open  the file  item-post.php  in your theme folder

To add the counter to the title - find this code

Code: [Select]
<div class="control-group">
                            <label class="control-label" for="title[<?php echo osc_locale_code(); ?>]"><?php _e('Title''bender'); ?></label>
                            <div class="controls">
                                <?php ItemForm::title_input('title',osc_locale_code(), osc_esc_htmlbender_item_title() )); ?>
                            </div>
                        </div>

And replace it with this Code

Code: [Select]
<div class="control-group">
                            <label class="control-label" for="title[<?php echo osc_locale_code(); ?>]"><?php _e('Title''bender'); ?></label>
                            <div class="controls">
                                <?php ItemForm::title_input('title',osc_locale_code(), osc_esc_htmlbender_item_title() )); ?>
Counter: <span id="char2"></span>
<script type="text/javascript">
var title_Limit = 25;
$('#title<?php echo osc_locale_code()?>').keyup(function() {
    if ($(this).val().length > title_Limit) {
         $(this).val($(this).val().substr(0, title_Limit));
       
    }
     $('#char2').text(this.value.replace(/{.*}/g, '').length);
});
</script>
                            </div>
                        </div>

And then change the value "25" to the characters limit that you want!

---------------------------------------------------------------------------------------------------------------------

Next we will add the counter and the limit to the description box- Find this code

Code: [Select]
<div class="control-group">
                            <label class="control-label" for="description[<?php echo osc_locale_code(); ?>]"><?php _e('Description''bender'); ?></label>
                            <div class="controls">
                                <?php ItemForm::description_textarea('description',osc_locale_code(), osc_esc_htmlbender_item_description() )); ?>
                            </div>
                        </div>


And replace it with this one
Code: [Select]
   <div class="control-group">
                            <label class="control-label" for="description[<?php echo osc_locale_code(); ?>]"><?php _e('Description''bender'); ?></label>
                            <div class="controls">
                                <?php ItemForm::description_textarea('description',osc_locale_code(), osc_esc_htmlbender_item_description() )); ?>
                                        Counter: <span id="charCount"></span>
<script type="text/javascript">
var description_Limit = 60;
$('#description<?php echo osc_locale_code()?>').keyup(function() {
    if ($(this).val().length > description_Limit) {
         $(this).val($(this).val().substr(0, description_Limit));
       
    }
     $('#charCount').text(this.value.replace(/{.*}/g, '').length);
});
</script>
   </div>
                        </div>

And then change the value "60" to the characters limit that you want for description!

-------------------------------------------------------------------------------------------------------------------

P.S. Your limit from the admin panel should be equal or higher than the limit you set for the counter - for example if you want to make title limit to 25 characters  be sure that u set it to 25 or more   in admin panel  (30,35... all will work) so you avoid getting error when publishing the ad.


Here you got it :) Hope you enjoyed this tutorial...


If you want to know how to enable numbers only in price field check our other tutorial
http://forums.osclass.org/tips-and-tricks/%28tutorial%29-allow-numbers-only-in-price-field/

Regards, ibthemes.com
« Last Edit: February 07, 2014, 10:40:01 am by Ibthemes.com »

gnoe

  • Full Member
  • ***
  • Posts: 237
Great mod and works fine.
Thanks Ibthemes  ;)

zoopla

  • Guest
Great mod and works fine.
Thanks Ibthemes  ;)

Glad you found it useful, Hopefully more tips and tutorials will follow... :)

Regards,
ibthemes.com


serjuc11111

  • Hero Member
  • *****
  • Posts: 814
this works only for bender theme!For modern is a lot to work!

Maximus

  • Newbie
  • *
  • Posts: 41
Hi, serjuc11111!

Were you able to this tutorial for modern theme?

If yes, could you share your solution with us?

Thank you in advance.

zoopla

  • Guest
this works only for bender theme!For modern is a lot to work!

Hi i have mentioned that its for bender theme - but you can use a similar aproach for modern theme

zoopla

  • Guest
Hi, serjuc11111!

Were you able to this tutorial for modern theme?

If yes, could you share your solution with us?

Thank you in advance.

Im gonna make another post that will explain the process for modern theme soon

haryour

  • Newbie
  • *
  • Posts: 36
  • My fingers and keyboard are magno-reacting
@Ibthemes.com
I tried this on bender theme and it works fine.

Just a question, what if I want the number to count down?
« Last Edit: March 03, 2014, 11:57:30 pm by haryour »

kernelpanicit

  • Newbie
  • *
  • Posts: 4
this works only for bender theme!For modern is a lot to work!

No, no, no, no, no, .... ----> Vers.: 3.2.2 (it suppose to doesn't matter) on my custom theme from "Italia Theme" (should work with "Modern" aswell) here the code:

Code: [Select]
<div class="row">
<div class="control-group">
                           
                            <div class="controls">
                                <?php ItemForm::multilanguage_title_description(); ?>

<script type="text/javascript">
var title_Limit = 25;
$('#title<?php echo osc_locale_code()?>').keyup(function() {
    if ($(this).val().length > title_Limit) {
         $(this).val($(this).val().substr(0, title_Limit));
       
    }
     $('#char2').text(this.value.replace(/{.*}/g, '').length);
});


</script>
<script type="text/javascript">
var description_Limit = 450;
$('#description<?php echo osc_locale_code()?>').keyup(function() {
    if ($(this).val().length > description_Limit) {
         $(this).val($(this).val().substr(0, description_Limit));
       
    }
     $('#charCount').text(this.value.replace(/{.*}/g, '').length);
});
 </script></br>*Max char admitted (25): <span id="char2"></span></br>*Max char admitted (450): <span id="charCount"></span></br></br>

                            </div>
You can adapt it to any field replacing:  <?php ItemForm::multilanguage_title_description(); ?> with your field formula, erase <?php echo osc_locale_code()?>change values name and put correct #id as ex: $('#description).keyup(function() {

Just use your imagination  ::)  :-*
« Last Edit: June 19, 2014, 04:40:06 am by kernelpanicit »