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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Adding character count...
Power Pages
Unanswered

Adding character counter to a form field

(0) ShareShare
ReportReport
Posted on by 323

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)
  • Lucas001 Profile Picture
    2,484 Super User 2025 Season 2 on at

    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.

  • Fubar Profile Picture
    8,435 Super User 2025 Season 2 on at

    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		
     
     });
    }

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Pages

#1
Fubar Profile Picture

Fubar 93 Super User 2025 Season 2

#2
Jerald Felix Profile Picture

Jerald Felix 45

#3
Lucas001 Profile Picture

Lucas001 28 Super User 2025 Season 2

Last 30 days Overall leaderboard