web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id : dmvc6UkFKwfmPWwaxdYLNo
Power Pages - General Discussions
Unanswered

Adding character counter to a form field

Like (0) ShareShare
ReportReport
Posted on 3 May 2024 13:24:08 by 311

Hi,

 

We have a requirement to add a character counter to a form field in Power pages. How can we achieve this?

 

prathyoo_0-1714742524958.png

 

Example, like this - 

prathyoo_1-1714742586000.png

 

Categories:
I have the same question (0)
  • Fubar Profile Picture
    8,060 Super User 2025 Season 2 on 07 May 2024 at 23:29:03
    Re: Adding character counter to a form field

    This is one I did not that long ago, you just call it and pass it the schema name of the field, it will also check if someone is pasting too much text in (if there's browser support) - no warranty is provided.

    function addCharacterCounter(fieldName){
     //debugger;
     let maxLength = $("#"+fieldName).attr("maxlength");
     let formattedMaxLength = Number(maxLength).toLocaleString("en-US");
     let currentLength = $("#"+fieldName).val().length.toLocaleString("en-US");
     $( "<p style='float: right;'><span id='"+fieldName+"CharCounter'>"+currentLength+"</span><span>/"+formattedMaxLength+"</span></p>" ).insertAfter($("#"+fieldName))
    	
     // update counter on key event
     $( "#"+fieldName ).on( "keyup", function(event) {
     let fieldId = event.currentTarget.id;
     let newLength = $("#"+fieldId).val().length.toLocaleString("en-US") ;		
     $("#"+fieldId+"CharCounter").text(newLength);		
     });
    	
    	
     // put message on screen if attempt to paste too much text
     // also takes into account pasting to replace selected text
     const target = document.getElementById(fieldName);
     target.addEventListener("paste", (e) => {
     try{
     	 let paste = (e.clipboardData || window.clipboardData).getData("text");
    	 let clipLength = paste.length;
    	 let existingLength = $("#"+e.currentTarget.id).val().length;		
    	 let maxLength = Number($("#"+e.currentTarget.id).attr("maxlength"));
    
    	 // if there is text that is currently selected that will be replaced 
     // get the length of the selection
    	 let selectedTextLength = window.getSelection().toString().length;
    		
     	 if((clipLength+existingLength-selectedTextLength) > maxLength ){
    	 alert("Unable to paste content - content will exceed the maximum length for the field");
     return false;
    	 }
     }
     catch(err){
    	 //do nothing
     }		
    		
     return true		
     
     });
    }

     

  • Lucas001 Profile Picture
    2,268 Super User 2025 Season 2 on 07 May 2024 at 09:53:55
    Re: Adding character counter to a form field

    Hi @prathyoo,

     

    I think that this codepen will be helpfull: https://codepen.io/btn-ninja/pen/QWGRRZE

    You probably need to add the p tag by using jQuery and insert it below your field.

     

    The JS should also work fine inside your page.

    I am not aware of an OOB Solution for your requirement.

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

    Hope that helps.

    If the post solves your problem, please mark it as solution. If it helps, consider a thumbs up.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Telen Wang – Community Spotlight

We are honored to recognize Telen Wang as our August 2025 Community…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 62 Super User 2025 Season 2

#2
Lucas001 Profile Picture

Lucas001 48 Super User 2025 Season 2

#3
KevinGador Profile Picture

KevinGador 44 Super User 2025 Season 2