File tree Expand file tree Collapse file tree 1 file changed +15
-13
lines changed
Client-Side Components/Client Scripts/Display a Live Word Count for Description Field Expand file tree Collapse file tree 1 file changed +15
-13
lines changed Original file line number Diff line number Diff line change 11function onLoad ( ) {
2- var field = g_form . getControl ( 'description' ) ;
3- var counter = document . createElement ( 'div' ) ;
4- counter . id = 'desc_word_counter' ;
5- counter . style . marginTop = '5px' ;
6- field . parentNode . appendChild ( counter ) ;
2+ var fieldName = 'description' ;
73
8- field . addEventListener ( 'input' , function ( ) {
9- var wordCount = field . value . trim ( ) . split ( / \s + / ) . length ;
10- counter . innerText = 'Word Count: ' + ( field . value ? wordCount : 0 ) ;
11- if ( wordCount > 150 ) {
12- counter . style . color = 'red' ;
13- } else {
14- counter . style . color = 'green' ;
15- }
4+ // Clear any existing messages on load
5+ g_form . hideFieldMsg ( fieldName , true ) ;
6+
7+ g_form . getControl ( fieldName ) . addEventListener ( 'input' , function ( ) {
8+ var fieldValue = g_form . getValue ( fieldName ) . trim ( ) ;
9+ var wordCount = fieldValue ? fieldValue . split ( / \s + / ) . length : 0 ;
10+
11+ var message = 'Word Count: ' + wordCount ;
12+ var type = ( wordCount > 150 ) ? 'error' : 'info' ; // red for error, greenish for info
13+
14+ // Clear previous message before showing new one
15+ g_form . hideFieldMsg ( fieldName , true ) ;
16+
17+ g_form . showFieldMessage ( fieldName , message , type ) ;
1618 } ) ;
1719}
You can’t perform that action at this time.
0 commit comments