我想将 textarea 字符长度限制为 150 个字符,我的 system.xml 代码如下:

I want to restrict textarea characters length to 150 characters, My system.xml code is below :

我想将文本区域字符长度限制为 150 个字符,我的 system.xml 代码如下:

...
<orderPlaceMessage translate="label">
    <label>Message for order place: </label>
    <frontend_type>textarea</frontend_type>
    <sort_order>1</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>

    {how to limit character length }

</orderPlaceMessage>
...

您可以使用 <validate> 标签将文本区域字符长度限制为 150 个字符。

<validate>validate-length maximum-length-150</validate>

您也可以添加评论,让用户知道所需的范围是多少。

请在下面找到两个解决方案。

方案一:-

将以下内容添加到 <orderPlaceMessage> 元素:

<validate>validate-length maximum-length-150</validate>

This will add these validation CSS classes used by prototype.js. If you enter a longer value, you will see this generic validation message:

So it is a good idea to add a comment as well to let the user know what this range is:

<comment>Maximum length: 150 characters</comment>

If the limit is important for data integrity, you should also add server-side validation, using a backend model. Digital Pianism already linked a tutorial for this: http://alanstorm.com/magento_system_config_validation

And if you want to know more about the various options in the system.XML, there is: http://alanstorm.com/magento_system_configuration_in_depth_tutorial

方案二:-

Please used below jQuery script for limit character length :

jQuery("#sms_cnfg_advanced_settings textarea").keypress(function(event){
    var maxLength = 160;
    var length = this.value.length;
    if (length >= maxLength)
     {
        this.value = this.value.substring(0, maxLength);
        alert(maxLength + ' characters allowed, excess characters trimmed');
     }
});

jQuery("#sms_cnfg_advanced_settings textarea").on('keyup',function(){
    jQuery('.charnum').remove();
    var maxLength = 160;
    var length = this.value.length;

     var count=maxLength-length;
     jQuery('<span class="charnum">' +count+' Characters left</span>').insertAfter(jQuery(this));

}); 

希望对你有所帮助