Limits the number of characters in textarea, for Rails - Javascript

I would like to limit the number of characters in textarea .

I found that the following Javascript code works well for a simple HTML file:

 <script language="javascript" type="text/javascript"> function DjCheckMaxlength(oInObj) { var iMaxLen = parseInt(oInObj.getAttribute('maxlength')); var iCurLen = oInObj.value.length; if ( oInObj.getAttribute && iCurLen > iMaxLen ) { oInObj.value = oInObj.value.substring(0, iMaxLen); } } //@ END OF DjCheckMaxlength() </script> <body> <input type="text" name="T1" size="20" maxlength="20" > <br /><hr /> <textarea maxlength="10" onkeyup="return DjCheckMaxlength(this);"></textarea> </body> 

What is the best way to use it inside a Rails application?

Thanks!

+4
source share
2 answers

Essentially the same should work.

 <%= text_area 'comment', 'body', :onkeyup => "DjCheckMaxlength(this);", :maxlength => 30 %> 

If you want to link externally:

 <%= javascript_include_tag "my-functions" %> 

Would get a JS file from public / javascripts / my-functions.js

+11
source

You can decode it in ascii and check the length.

0
source

Source: https://habr.com/ru/post/1344748/


All Articles