ShaharA Posted February 27, 2012 Share Posted February 27, 2012 Hi, Can any one tell me how do i restrict the length of a gift message to a maximum of 400 characters ? Thanks a lot Shahar Link to comment Share on other sites More sharing options...
Nuvish Posted February 27, 2012 Share Posted February 27, 2012 Try to restrict the length of the textarea in order-carier.tpl. Something like this may work : $(document).ready(function() { $('textarea[maxlength]').keyup(function(){ //get the limit from maxlength attribute var limit = parseInt($(this).attr('maxlength')); //get the current text inside the textarea var text = $(this).val(); //count the number of characters in the text var chars = text.length; //check if there are more characters then allowed if(chars > 20){ //and if there are use substr to get the text before the limit var new_text = text.substr(0, limit); //and change the current text with the new text $(this).val(new_text); } }); }); And don't forget to add maxlength to the text area. Example : <p><textarea cols="120" rows="3" name="message" id="message" maxlength="20">{if isset($oldMessage)}{$oldMessage}{/if}</textarea></p> Link to comment Share on other sites More sharing options...
ShaharA Posted February 27, 2012 Author Share Posted February 27, 2012 Thank you for your quick respond. Does it matter where i put this code in order-carrier.tpl? Wherever i tried to put it, it didn't work - i still get maxlength="400" as an Error. Does it have anything to do with var names as i use them: <textarea rows="5" cols="35" id="gift_message" name="gift_message" maxlength="400" >{$cart->gift_message|escape:'htmlall':'UTF-8'}</textarea> Thanks, Shahar Link to comment Share on other sites More sharing options...
Nuvish Posted March 8, 2012 Share Posted March 8, 2012 Hello. Generally,javascript is added to the top of the page. I don't know which prestashop version you are using but put these codes : <script type="text/javascript"> {literal} $(document).ready(function() { $('textarea[maxlength]').keyup(function(){ var limit = parseInt($(this).attr('maxlength')); var text = $(this).val(); var chars = text.length; if(chars > 20){ alert("Maxlength for gift message is 20 characters. ") var new_text = text.substr(0, limit); $(this).val(new_text); } }); }); {/literal} </script> before : {if !$opc} {capture name=path}{l s='Shipping'}{/capture} {include file="$tpl_dir./breadcrumb.tpl"} {/if} And replace this line : <textarea rows="5" cols="35" id="gift_message" name="gift_message">{$cart->gift_message|escape:'htmlall':'UTF-8'}</textarea> By : <textarea rows="5" cols="35" id="gift_message" name="gift_message" maxlength="20">{$cart->gift_message|escape:'htmlall':'UTF-8'} </textarea> This works for me. This is an example for limitation upto 20 characters. Change 20 to 400 after you tested it. Sorry for late reply :S Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now